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 |
|
MusselmanC
Starting Member
4 Posts |
Posted - 2009-08-27 : 11:27:49
|
| I may have been oversimplistic with my 'RECURSIVE CTE' post yesterday as I'm not fully able to get what I need..ORIGINAL POSTI need to be able to calculate a 'future inventory' qty derived table or CTE (I'll refer to it as FUTURE_INVEN) for an upcoming set of dates. Have 3 tables: PROD(Forecast Production), SALES(Open Sales Orders) and INVEN(Current Inventory) with qty's for future dates in PROD and SALES tables.Thought is that this can be done relatively easily via a recursive CTE but I can't seem to figure it out.In example data below, 8/25/09 FUTURE_INVEN = 8/24/09 INVEN + 8/25/09 PROD + 8/25/09 SALES8/26/09 FUTURE_INVEN = 8/25/09 FUTURE_INVEN + 8/26/09 PROD + 8/26/09 SALESEtc...PROD table DATE ___SKU _QTY8/24/09 1012 78/25/09 1012 128/26/09 1012 68/29/09 1012 99/3/09 1012 14SALES table DATE ___SKU _QTY8/24/09 1012 -158/25/09 1012 -118/26/09 1012 -98/28/09 1012 -129/1/09 1012 -6INVEN table DATE ___SKU _QTY8/24/09 1012 50Here's what I need...FUTURE_INVEN DATE ___SKU _QTY8/24/09 1012 428/25/09 1012 438/26/09 1012 408/27/09 1012 408/28/09 1012 378/29/09 1012 468/30/09 1012 468/31/09 1012 469/1/09 1012 409/2/09 1012 409/3/09 1012 54CLARIFICATION (where I oversimplifed)See additional data lines in read above -- having an issue with the inner joins as I will not always have a record for a given Date/SKU in the PROD and SALES tables=== ORIGINAL REPLY TO MY POST ===declare @PROD table (DATE datetime, SKU int, QTY int)insert @prodselect '8/24/09', 1012, 7 union allselect '8/25/09', 1012, 12 union allselect '8/26/09', 1012, 6declare @SALES table (DATE datetime, SKU int, QTY int)insert @salesselect '8/24/09', 1012, -15 union allselect '8/25/09', 1012, -11 union allselect '8/26/09', 1012, -9declare @INVEN table (DATE datetime, SKU int, QTY int)insert @invenselect '8/24/09', 1012, 50;with cte as( select ca.Date ,i.sku ,ca.qty from ( select sku from @inven i group by sku ) i cross apply ( select top 1 Date, Qty from @inven where sku = i.sku order by date desc ) ca union all select p.date ,i.sku ,i.qty + isNull(p.qty, 0) + isNull(s.qty,0) from cte i inner join @prod p on p.sku = i.sku and p.date = dateadd(day, 1, i.date) inner join @sales s on s.sku = i.sku and s.date = p.date)select date, sku, qty from cteoutput:date sku qty----------------------- ----------- -----------2009-08-24 00:00:00.000 1012 502009-08-25 00:00:00.000 1012 512009-08-26 00:00:00.000 1012 48Be One with the OptimizerTG --------------------------------------------------------------------------------Edited by - TG on 08/26/2009 13:37:31 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-27 : 11:38:17
|
| Two things, MusselmanC1) You should have continued with your original topic as it is still the same issue.2) Why should we have to add all your new data to the executable code I provided yesterday. Now that you have an example you can see how to add more rows to the sameple data - you're the one that needs the help - you should at least do that work.So add the sample data yourself and post the expected results based on your data.Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-08-27 : 12:33:54
|
| [code]SELECT DATE ,SKU,QTY INTO #tempFROM(SELECT DATE ,SKU ,QTYFROM PRODUNION ALLSELECT DATE ,SKU ,QTYFROM SALESUNION ALLSELECT DATE ,SKU ,QTYFROM INVEN)tDECLARE @Start datetime,@End datetimeSELECT @Start=MIN(DATE),@End=MAX(DATE)FROM #temp ;With Date_CTE(DateVal)AS(SELECT @StartUNION ALLSELECT DATEADD(dd,1,DateVal)FROM Date_CTEWHERE DATEADD(dd,1,DateVal)<=@End)SELECT d.DATE,d.SKU,d1.QtySumFROM Date_CTE dOUTER APPLY(SELECT SKU,SUM(QTY) AS QtySum FROM #temp WHERE Date<=d.Date GROUP BY SKU )d1[/code] |
 |
|
|
MusselmanC
Starting Member
4 Posts |
Posted - 2009-08-27 : 16:22:43
|
| Here's revised executable code for my scenario:============declare @PROD table (DATE datetime, SKU int, QTY int)insert @prodselect '8/24/09', 1012, 7 union allselect '8/25/09', 1012, 12 union allselect '8/26/09', 1012, 6 union allselect '8/29/09', 1012, 9 union allselect '9/3/09', 1012, 14declare @SALES table (DATE datetime, SKU int, QTY int)insert @salesselect '8/24/09', 1012, -15 union allselect '8/25/09', 1012, -11 union allselect '8/26/09', 1012, -9 union allselect '8/28/09', 1012, -12 union allselect '9/1/09', 1012, -6declare @INVEN table (DATE datetime, SKU int, QTY int)insert @invenselect '8/24/09', 1012, 50============expected results are:DATE SKU QTY8/24/09 1012 428/25/09 1012 438/26/09 1012 408/27/09 1012 408/28/09 1012 378/29/09 1012 468/30/09 1012 468/31/09 1012 469/1/09 1012 409/2/09 1012 409/3/09 1012 54Thanks in advance for any help you can provide. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-27 : 18:19:00
|
Thanks for adding the data and posting expected results. This doesn't exactly match your results. Not sure if it is your sample of if I'm missing something but this should be close. declare @PROD table (DATE datetime, SKU int, QTY int)insert @prodselect '8/24/09', 1012, 7 union allselect '8/25/09', 1012, 12 union allselect '8/26/09', 1012, 6 union allselect '8/29/09', 1012, 9 union allselect '9/3/09', 1012, 14declare @SALES table (DATE datetime, SKU int, QTY int)insert @salesselect '8/24/09', 1012, -15 union allselect '8/25/09', 1012, -11 union allselect '8/26/09', 1012, -9 union allselect '8/28/09', 1012, -12 union allselect '9/1/09', 1012, -6declare @INVEN table (DATE datetime, SKU int, QTY int)insert @invenselect '8/24/09', 1012, 50declare @last int, @firstDay datetimeselect @last = datediff(day, min(date), max(date)), @firstDay = min(date)from (select date from @prod union select date from @sales) d;with n (number) as( select 0 union all select n.number + 1 from n where n.number+1 <= @last),s (sku) as( select sku from @inven group by sku)select dateadd(day, n.number, @firstDay) Date ,s.sku ,oa.qtyfrom ncross join s outer apply ( select sum(qty) qty from ( select qty from @sales where sku = s.sku and date <= dateadd(day, n.number, @firstDay) union all select qty from @prod where sku = s.sku and date <= dateadd(day, n.number, @firstDay) union all select qty from @inven where sku = s.sku and date <= dateadd(day, n.number, @firstDay) ) u ) oaOUTPUT:Date sku qty----------------------- ----------- -----------2009-08-24 00:00:00.000 1012 422009-08-25 00:00:00.000 1012 432009-08-26 00:00:00.000 1012 402009-08-27 00:00:00.000 1012 402009-08-28 00:00:00.000 1012 282009-08-29 00:00:00.000 1012 372009-08-30 00:00:00.000 1012 372009-08-31 00:00:00.000 1012 372009-09-01 00:00:00.000 1012 312009-09-02 00:00:00.000 1012 312009-09-03 00:00:00.000 1012 45 Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|