我正在尝试为多个可变销售中心提取每个月的销售业务数量。使用以下 TSQL ...
;WITH Months(m) AS
(
SELECT 1 m
UNION ALL
SELECT m+1 FROM Months WHERE m < 12
)
SELECT t.Center,m Month, t.Sales FROM Months
CROSS APPLY
(
SELECT C.Center, COUNT(1) Sales FROM Operations C
LEFT JOIN Centers A ON A.Code=C.Center
WHERE Date BETWEEN '01/'+ CONVERT(VARCHAR(2),Months.m) + '/2013' AND DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'01/'+ CONVERT(VARCHAR(2),Months.m) + '/2013')+1,0))
GROUP BY C.Center
) t
所以我得到以下输出:
Center Month Sales
-----------------------
A 1 20
B 1 30
A 2 25
B 2 30
....
我想结束的是:
Center 1 2 ...
----------------------
A 20 25 ...
B 30 30 ...
我正在用 xmlpath 研究枢轴,但它太复杂了,我无法让它工作。有人有解决方案吗?