我有一個記錄倉庫數據庫事務的表。本表中的信息包括交易發生地點,交易結束日期,結束時間,轉移數量和分部。我想獲得: - 任何單位和交易,爲每月每部這樣的:按月分組在SQL中顯示重複的月份
Division Month Transactions Units
1 January 5 10 1 February 10 11 2 February 5 10
我的SQL查詢如下:
SELECT
DISTINCT tranl.Division,
DATENAME(month,tranl.trandate) AS 'Month',
COUNT(tranl.qty) AS Transactions,
SUM(tranl.qty) AS Units
FROM translog (nolock) tranl
WHERE tranl.location = 'stage'
AND end_tran_date >= '2013-04-01 00:00:00.000'
GROUP BY end_tran_date, tranl.Division
ORDER BY tranl.Division
,但我得到了不同的結果:
Division Month Transactions Units 1 January 5 10 1 January 3 22 2 February 10 40 2 February 3 12 2 March 1 1 ....
你可以給SQL來創建DW表,也可能是一些示例行嗎? –