使用 SQL Server 2008 R2。我不确定这是否可行,但我想按两列分组,并将它们的差异放入新列中。用一个例子可能更容易解释:
前:: [日期] [ID] [AMT] [类型] 2013-01-11 36374DCD-47FE-48D8-8E70-8D3B37385311 2 20 2013-01-11 36374DCD-47FE-48D8-8E70-8D3B37385311 10 21 2013-01-11 4434E2D5-1D08-45FA-AADF-F653BF9A0D97 4 20 2013-01-11 4434E2D5-1D08-45FA-AADF-F653BF9A0D97 4 21 后:: [日期] [ID] [AMT 20] [AMT 21] 2013-01-11 36374DCD-47FE-48D8-8E70-8D3B37385311 2 10 2013-01-11 4434E2D5-1D08-45FA-AADF-F653BF9A0D97 4 4
这是我到目前为止的 SQL:
SELECT
CAST(TransDate AS DATE) AS [TransDate],ItemID,COUNT(TransactionTypeID) AS [TransAmt], TransactionTypeID
FROM
Transactions
WHERE
TransDate BETWEEN '2013-01-01 10:00:00' AND '2013-02-01 10:00:00'
AND TransactionTypeID IN (20,21)
GROUP BY
CAST(TransDate AS DATE),ItemID,TransactionTypeID
任何帮助表示赞赏,谢谢!