Author |
Topic |
inbs
Aged Yak Warrior
860 Posts |
Posted - 2010-10-07 : 08:48:04
|
i have 2 tables:Orders:OrderDate OrderEndDate TotalDays01-01-2010 05-01-2010 402-01-2010 10-01-2010 8 05-01-2010 07-01-2010 210-01-2010 13-01-2010 3 holidaysHolidayDate Days03-01-2010 109-01-2010 1 so i want to update Orders Table,i mean that i want to get this table:OrderDate OrderEndDate TotalDays01-01-2010 05-01-2010 (4-1=)302-01-2010 10-01-2010 (8-1-1=)6 05-01-2010 07-01-2010 210-01-2010 13-01-2010 3 (no holidays between dates) how i can get it by query? |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-07 : 09:10:49
|
Why are you subtracting for this?05-01-2010 07-01-2010 (2-1=)1 PBUH |
 |
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2010-10-07 : 09:11:58
|
sorry my mistake |
 |
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2010-10-07 : 09:14:14
|
i try something like that ,but it is not work:(each order i need to check if was holiday between OrderDate ans OrderEndDate).DECLARE @HolidayDate DateTime,@Days intDECLARE Holiday_Cursor CURSOR FORSELECT * FROM holidaysOPEN Holiday_CursorWHILE @@fetch_status=0BEGIN UPDATE o SET o.TotalDays = o.TotalDays - 1 FROM Orders o JOIN holidays h ON h.HolidayDate Between o.OrderDate AND o.OrderEndDate fetch next from Holiday_Cursor into @HolidayDate ,@DaysENDclose Holiday_Cursordeallocate Holiday_Cursor |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-07 : 09:23:29
|
[code]set dateformat dmygodeclare @o table(OrderDate date, OrderEndDate date,TotalDays int)insert into @oselect '01-01-2010' , '05-01-2010', 4 union select '02-01-2010' , '10-01-2010' , 8 union select '05-01-2010' , '07-01-2010' , 2 union select '10-01-2010' , '13-01-2010' , 3declare @h table(HolidayDate date,Days int)insert into @hselect '03-01-2010' , 1 unionselect '09-01-2010' , 1select OrderDate, OrderEndDate, isnull(TotalDays-SUM(days),TotalDays)TotalDays from @oouter apply( select holidaydate,days from @h where HolidayDate between OrderDate and OrderEndDate )hgroup by OrderDate,OrderEndDate,TotalDays[/code]PBUH |
 |
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2010-10-07 : 09:52:01
|
why not use cursor?and i use sql 2000 and i can not use outer apply |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-07 : 13:02:02
|
quote: Originally posted by inbs why not use cursor?and i use sql 2000 and i can not use outer apply
Now you are telling me.... Why did you post it in SQL 2005 forum in the first place???PBUH |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-10-07 : 13:24:13
|
quote: why not use cursor?
Because cursors suck and they're not needed:SELECT O.OrderDate, O.OrderEndDate, O.TotalDays-IsNull((select sum(days) from holidays where holidaydate between O.OrderDate AND O.OrderEndDate),0) TotalDaysFROM Orders O Is there any reason you're storing TotalDays instead of calculating it from the 2 order dates? |
 |
|
inbs
Aged Yak Warrior
860 Posts |
Posted - 2010-10-10 : 01:48:36
|
Sachin.Nandsorry,i wrote here because this forum is more active than SQL 2K.robvolkthanks ,very good, ididnt thinks of that. |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-10-10 : 08:41:43
|
quote: sorry,i wrote here because this forum is more active than SQL 2K.
That doesn't help us answer your questions promptly or accurately, and wastes our time. Post in the forum most appropriate to your environment. |
 |
|
|