Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rchau861
Starting Member
5 Posts |
Posted - 2012-04-23 : 15:34:49
|
Hi guys...... please treat me as a SQL noobieI have a question. Lets take a simple queryselect date,amount,categoryfrom abcorder by date descOutput looks like this:Date Amount Category4/13/2012 1000 a4/12/2012 800 b4/11/2012 700 c4/10/2012 650 c4/09/2012 600 d4/05/2012 500 aSimple right....... Lets treat these days as business days. I need to now include non business days as well. (weekends and holidays)The requirement is now that if the date does not exist, take the last known business day and copy that record and date it with the non business daySo using the information provided and using the date 4/06/2012 as an example, I would need to copy the record with 4/05/2012 and insert it as 4/06/2012. The same would apply to 04/07/2012 and 04/08/2012 that would need to "copy" the last known business date being 04/05/2012 and dating it with 04/07/2012 and 04/08/2012 respectively.Desired output:Take the above table or result set. I want this to display the missing dates of 4/6,4/7,4/8 with the last known business date being 4/05/2012.How do I accomplish this?How do I copy a row and change the date of that row?Desired ResultsDate Amount Category4/13/2012 1000 a4/12/2012 500 b4/11/2012 700 c4/10/2012 650 c4/09/2012 600 d4/08/2012 500 a4/07/2012 500 a4/06/2012 500 a4/05/2012 500 aSummary:I am creating a report in desc order by datePossible Solutions:Do i need to insert my result set into a temp table then use a cursor to step through each record and check if datediff(day,date,cursor date) > 1 ?? ( sorry my poor coding its been really long) If it is greater then 1 then I know it satisfies the condition of a "non business day"Any sample code would be appreciated..... please be simple for i am a noobie and forgive me if i accidently insulted you SQL gurus out there with my ignorant question. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-23 : 16:02:25
|
quote: Originally posted by rchau861 Hi guys...... please treat me as a SQL noobieI have a question. Lets take a simple queryselect date,amount,categoryfrom abcorder by date descOutput looks like this:Date Amount Category4/13/2012 1000 a4/12/2012 800 b4/11/2012 700 c4/10/2012 650 c4/09/2012 600 d4/05/2012 500 aSimple right....... Lets treat these days as business days. I need to now include non business days as well. (weekends and holidays)The requirement is now that if the date does not exist, take the last known business day and copy that record and date it with the non business daySo using the information provided and using the date 4/06/2012 as an example, I would need to copy the record with 4/05/2012 and insert it as 4/06/2012. The same would apply to 04/07/2012 and 04/08/2012 that would need to "copy" the last known business date being 04/05/2012 and dating it with 04/07/2012 and 04/08/2012 respectively.Desired output:Take the above table or result set. I want this to display the missing dates of 4/6,4/7,4/8 with the last known business date being 4/05/2012.How do I accomplish this?How do I copy a row and change the date of that row?Desired ResultsDate Amount Category4/13/2012 1000 a4/12/2012 500 b4/11/2012 700 c4/10/2012 650 c4/09/2012 600 d4/08/2012 500 a4/07/2012 500 a4/06/2012 500 a4/05/2012 500 aSummary:I am creating a report in desc order by datePossible Solutions:Do i need to insert my result set into a temp table then use a cursor to step through each record and check if datediff(day,date,cursor date) > 1 ?? ( sorry my poor coding its been really long) If it is greater then 1 then I know it satisfies the condition of a "non business day"Any sample code would be appreciated..... please be simple for i am a noobie and forgive me if i accidently insulted you SQL gurus out there with my ignorant question.
you can do like thisDECLARE @Min datetime,@Max datetimeSELECT @MIN=MIN([Date]),@MAX=MAX([Date])FROM YourtableSELECT [Date],[Amount],[Category]FROM YourTableUNION ALLSELECT f.[Date],t1.Amount, t1.CategoryFROM dbo.CalendarTable(@MIN,@MAX,0,0) fLEFT JOIN YourTable tON f.[Date] = t.[Date]CROSS APPLY (SELECT TOP 1 Amount, Category FROM YourTable WHERE [Date] < f.[Date] ORDER BY [Date] DESC )t1WHERE t.[Date] IS NULLORDER BY [Date] CalendarTable function can be seen herehttp://visakhm.blogspot.com/2010/02/generating-calendar-table.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
rchau861
Starting Member
5 Posts |
Posted - 2012-04-23 : 16:27:40
|
ty for your reponse visakh16!!!!I will definitely look into recursive CTEthank you for pointing me in the right direction. I have working knowledge of SQL but I am no expert by any means.I need to do more reading to understand your solution but cheers for replying to me |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-23 : 19:00:14
|
i've not used CTE. i've used only a UDF------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|