Sunday, 15 May 2011

SQL Rollup last 4 weeks total -


I have a table, which I want to get in the last four weeks but I can call it a SELECT (the last 4 Want to return with order 1 column of weeks - if they exist).

  purchasing ID order 1 date charvecotal ----------- - ------------------- ---- --- --------------- 1 1.00 2013-04-21 14.00 2 2.00 2013-04-14 12.00 3 3.00 2013-04-07 9.00 4 4.00 2013-03-31 5.00 5 5.00 2013-03-24 0.00   

My understanding of each record in my table That is, you want to see the sum of Order1 for yourself and record each record for the date value within four weeks before the primary. Here you go: << p>

  Create table misestablishment (PurchasingId int no zero primary key identification (1,1), order 1 plus null, [Date] date is not zero Insert MisterTable (Order 1, [Date] Price (1.00, '2013-04-21') Insert MisTable (Order 1, [Date]) Price (2.00, '2013-04-14') Insert Missterable (Order 1 Value (3.00, '2013 Microsoft (Order 1, [Date]) Price (5.00,' 2013-03-24 ') MisterTable (Order 1, [Date]) Price (4.00,' 2013-03 -31 ') Summum Join PurchasingId, t1.Order1, t1.Date, SUM (ISNULL (t2.Order1, 0)) MysteryTable T1 in total intersection join MisterTable T2 at DATEDD (ww, -4, t1.Date) & lt; = T2.Date and T1.Date & gt; T2.Date Group by t1.PurchasingId, t1.Order1, t1.Date order by t1.Date desc   

Explanation:

Join yourself on the table, T1 to return the records, join TB to record the T2, on the basis of the date of T1, four weeks of loan, less than the date of T2 or its Date of date and T2 Mr. more from the date of 2. Then the T1 field and the amount recorded by T2.Order1 group. The left external insertion account for a record in which there will be no predecessor data.

No comments:

Post a Comment