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 |
|
deanfp
Starting Member
26 Posts |
Posted - 2012-07-16 : 06:25:48
|
| HiI have two SQL Scripts that I run to track sales of our products. The first below shows us the product SKU, quantity and amount sold. The second shows what Store ID each SKU is mapped to. How can I join both so that the SKU's StoreID is shown in the first Script?Script 1USE [multistorenc]GOselect os.OrderedProductSKU, convert(DECIMAL(15,2),SUM(os.OrderedProductPrice)) totalsales,cast(os.OrderedProductName as nvarchar(255)) Name, SUM(os.Quantity) Qty from Orders_ShoppingCart osjoin Orders o on o.OrderNumber = os.OrderNumberwhere o.OrderDate between '2011-04-01 00:00:00.000' and '2012-04-01 00:00:00.000'group by os.OrderedProductSKU, cast(os.OrderedProductName as nvarchar(255))order by 2 descScript 2select p.SKU, p.Name, ps.StoreIDfrom dbo.Product p with (NOLOCK)join dbo.ProductStore ps with (NOLOCK) on ps.ProductID = p.ProductIDorder by p.SKU, ps.StoreIDThanks |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 06:45:27
|
| select ps.StoreID, os.OrderedProductSKU, convert(DECIMAL(15,2),SUM(os.OrderedProductPrice)) totalsales,cast(os.OrderedProductName as nvarchar(255)) Name, SUM(os.Quantity) Qtyfrom Orders_ShoppingCart osjoin Orders o on o.OrderNumber = os.OrderNumberjoin Product p on os.OrderedProductSKU = p.SKUjoin dbo.ProductStore ps on ps.ProductID = p.ProductIDwhere o.OrderDate between '2011-04-01 00:00:00.000' and '2012-04-01 00:00:00.000'group by os.OrderedProductSKU, cast(os.OrderedProductName as nvarchar(255))order by 2 descAre products unique by store?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
deanfp
Starting Member
26 Posts |
Posted - 2012-07-16 : 07:02:31
|
| HiYes they are assigned to one store. I tried to execute the script but got thisColumn 'dbo.ProductStore.StoreID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 07:44:47
|
| select StoreID = max(ps.StoreID), os.OrderedProductSKU, convert(DECIMAL(15,2),SUM(os.OrderedProductPrice)) totalsales,cast(os.OrderedProductName as nvarchar(255)) Name, SUM(os.Quantity) Qtyfrom Orders_ShoppingCart osjoin Orders o on o.OrderNumber = os.OrderNumberjoin Product p on os.OrderedProductSKU = p.SKUjoin dbo.ProductStore ps on ps.ProductID = p.ProductIDwhere o.OrderDate between '2011-04-01 00:00:00.000' and '2012-04-01 00:00:00.000'group by os.OrderedProductSKU, cast(os.OrderedProductName as nvarchar(255))order by 2 desc==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
deanfp
Starting Member
26 Posts |
Posted - 2012-07-16 : 07:48:51
|
| Perfect! THanks! I can see now how this is done. |
 |
|
|
|
|
|
|
|