如何按行列分组数据?
查询:
SELECT *
FROM
(
SELECT MONTH(T1.SPL_CreateDate) AS DateMonth,
T1.SPL_EngagementCount,
T1.SPL_ViewCount
FROM LP_SocialPostLog T1
) T2
GROUP BY T2.DateMonth, SPL_EngagementCount, SPL_ViewCount
结果:
DateMonth SPL_EngagementCount SPL_ViewCount
1 0 1
1 1 0
2 0 1
我想要结果:
DateMonth SPL_EngagementCount SPL_ViewCount
1 1 1
2 0 1
答案 0 :(得分:1)
如果您使用SUM
,则可以汇总其他两列
SELECT DateMonth,
sum(SPL_EngagementCount) SPL_EngagementCount,
sum(SPL_ViewCount) SPL_ViewCount
FROM (SELECT MONTH(T1.SPL_CreateDate) AS DateMonth,
T1.SPL_EngagementCount,
T1.SPL_ViewCount
FROM LP_SocialPostLog T1
) T2
GROUP BY T2.DateMonth
答案 1 :(得分:1)
也许这个:
SELECT Month(SPL_CreateDate) AS [DateMonth],
Sum(SPL_EngagementCount) AS [SPL_EngagementCount],
Sum(SPL_ViewCount) AS [SPL_ViewCount]
FROM LP_SocialPostLog
GROUP BY Month(SPL_CreateDate)
注意:如果您想要最大值,请使用max()
代替sum()
。
答案 2 :(得分:1)
我想你只想要这个?
SELECT MONTH(T1.SPL_CreateDate) AS DateMonth
,SUM(T1.SPL_EngagementCount) as SPL_EngagementCount
,SUM(T1.SPL_ViewCount) as SPL_ViewCount
FROM LP_SocialPostLog T1
GROUP BY MONTH(T1.SPL_CreateDate)