月份统计精简Sql
时间:2011-03-17 来源:懒人ABC
SELECT
Q1.ProductName
,(Q2.M1 & Power(2,OrderDate)) * SaleVolume AS M1
,(Q2.M2 & Power(2,OrderDate)) * SaleVolume AS M2
,(Q2.M3 & Power(2,OrderDate)) * SaleVolume AS M3
,(Q2.M4 & Power(2,OrderDate)) * SaleVolume AS M4
,(Q2.M5 & Power(2,OrderDate)) * SaleVolume AS M5
,(Q2.M6 & Power(2,OrderDate)) * SaleVolume AS M6
,(Q2.M7 & Power(2,OrderDate)) * SaleVolume AS M7
,(Q2.M8 & Power(2,OrderDate)) * SaleVolume AS M8
,(Q2.M9 & Power(2,OrderDate)) * SaleVolume AS M9
,(Q2.M10 & Power(2,OrderDate)) * SaleVolume AS M10
,(Q2.M11 & Power(2,OrderDate)) * SaleVolume AS M11
,(Q2.M12 & Power(2,OrderDate)) * SaleVolume AS M12
FROM
(
SELECT
T3.[name] AS ProductName
,datepart(MM, T2.orderdate) AS OrderDate
,SUM(ISNULL(T1.totalretailvalue,0.00)) AS SaleVolume
FROM [Order] T1
INNER JOIN
OrderPart T2
ON T1.id = T2.[orderno]
INNER JOIN
m_product T3
ON T2.collectionid = T3.id
WHERE T1.isdeleted = 0 AND T3.id = $ProductId$ AND datepart(YEAR, T2.orderdate) = $Year$
GROUP BY T3.[name],datepart(MM, T2.orderdate)
) Q1
CROSS JOIN
(
SELECT 1 AS M1,2 AS M2,4 AS M3,8 AS M4, 16 AS M5,32 AS M6,64 AS M7,128 AS M8, 256 AS M9, 512 AS M10, 1024 AS M11, 2048 AS M12
) Q2