如何将SQL Server表的垂直行记录转换为列数据。我尝试了几次查询,但无法达到结果。
我有一个名为TRANSACTIONLOG
的表,该表具有三列
CREATEDATE, TRANSACTION, COUNTS
我需要将CREATEDATE
上的记录分组,并将交易类型显示为一列,并将COUNTS显示为其值。
下面是结果的样子-
答案 0 :(得分:0)
这很容易进行一些条件聚合。
SELECT CREATEDATE
, [INSERT] = MAX(case when [TRANSACTION] = 'INSERT' then COUNTS end)
, [UPDATE] = MAX(case when [TRANSACTION] = 'UPDATE' then COUNTS end)
, [DELETE] = MAX(case when [TRANSACTION] = 'DELETE' then COUNTS end)
FROM TRANSACTIONLOG
GROUP BY CREATEDATE
ORDER BY CREATEDATE