Author |
Topic |
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-10-18 : 15:29:10
|
Hi All,I have a Table Called SalesHistory with the following fields:Saledate(datetime),ProductID(nvarchar),OutletID(nvarchar),Cost(decimal),Price(decimal),Units(BigInt)This table holds saleshistory information, that is how many units of a particular product was sold by which outlet on which date for how much price & cost.There's always only record of each product, per outlet, per date. Remeber each date is 1 week here.Here's the sample data:SaleDate,ProductID,OutletID,Cost,Price,Units2005-10-03,6,4,27.00,19.25,102005-10-10,6,4,20.00,19.25,202005-10-03,6,5,37.00,19.25,22005-10-10,6,6,30.00,19.25,10Now I have another table called ProductsWithMultiplePromoPrice with the following fieldsStartDate(datetime),RevertDate(datetime),ZoneID(nvarchar),ProductID(nvarchar),Price(decimal),Cost(decimal),OuletID(nvarchar)This table holds promotional price information for a particular product that was sold between a date range (startdate & revertdate) from a particular outlet in each zone. Here, if we look at product that was sold on a particular date in a particular zone we have multiple records in each zone, because of differnet outlets that exists in each zone, plus also big date ranges which may consists of multiple weeks.Here's the sample data:StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID2005-10-03,2005-10-29,1,6,5.69,4.76,42005-10-03,2005-10-29,1,6,5.69,4.76,52005-10-03,2005-10-29,1,6,5.69,4.76,6Now I want to get all fields from ProductsWithMultiplePromoPrice table, and I also want to see the total units (from SalesHistory table) that was sold by each outlet between a particular date range. For example I want to see the following:StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID,TotalUnitsHere's the sample data:StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID,TotalUnits2005-10-03,2005-10-29,1,6,5.69,4.76,4,302005-10-03,2005-10-29,1,6,5.69,4.76,5,22005-10-03,2005-10-29,1,6,5.69,4.76,6,10I would really appreciate if anyone can help me on that.Thanks, |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-18 : 16:17:05
|
Since your SalesHistory table only has sales at the week level, you will only be able to get sales for date ranges that start and end on week boundries. Is that the case here?CODO ERGO SUM |
 |
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-10-19 : 09:52:51
|
Well,I am not following exactly what are you saying.Look at this again.Here's the sample data for SalesHistory table:SaleDate,ProductID,OutletID,Cost,Price,Units2005-10-03,6,4,27.00,19.25,102005-10-10,6,4,20.00,19.25,202005-10-03,6,5,37.00,19.25,22005-10-10,6,6,30.00,19.25,10Here's the sample data for ProductsWithMultiplePromoPrice table:StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID2005-10-03,2005-10-29,1,6,5.69,4.76,42005-10-03,2005-10-29,1,6,5.69,4.76,52005-10-03,2005-10-29,1,6,5.69,4.76,6The output in this case, should be:StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID,TotalUnits2005-10-03,2005-10-29,1,6,5.69,4.76,4,302005-10-03,2005-10-29,1,6,5.69,4.76,5,22005-10-03,2005-10-29,1,6,5.69,4.76,6,10Look at the first row in the output:StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID,TotalUnits2005-10-03,2005-10-29,1,6,5.69,4.76,4,30We achive a total units of 30 because date ranges between 2005-10-03 and 2005-10-29 for productID 1 from outlet 4 we have the following two records in the saleshistory table.SaleDate,ProductID,OutletID,Cost,Price,Units2005-10-03,6,4,27.00,19.25,102005-10-10,6,4,20.00,19.25,20Thats why in the output for the first row we have this record.StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID,TotalUnits2005-10-03,2005-10-29,1,6,5.69,4.76,4,30Now, can someone please tell me how I can achieve this in T-SQL script.Thanks a million in advance.Z |
 |
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-10-19 : 10:18:02
|
I have got the soultion myself.select p.*,sum(sh.units)TotalUnitsfrom ProductsWithMultiplePromoPrice pjoin saleshistory shon (p.outletid=sh.outletid and p.productid=sh.productid and sh.saledate>=p.startdate and sh.saledate<=p.revertdate)group by p.startdate,p.revertdate,p.zoneid,p.productid,p.price,p.cost,p.outletidThis will give the following exact output I am looking for:StartDate,RevertDate,ZoneID,ProductID,Price,Cost,OuletID,TotalUnits2005-10-03,2005-10-29,1,6,5.69,4.76,4,302005-10-03,2005-10-29,1,6,5.69,4.76,5,22005-10-03,2005-10-29,1,6,5.69,4.76,6,10:) |
 |
|
|
|
|