Biletbak.com
Biletbak.com

Reputation: 417

To group data by rows column

How to group data by rows column?

query:

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

result:

DateMonth   SPL_EngagementCount SPL_ViewCount
1            0                   1
1            1                   0
2            0                   1

I want results:

DateMonth   SPL_EngagementCount SPL_ViewCount
1            1                   1
2            0                   1

Upvotes: 0

Views: 95

Answers (3)

Elliveny
Elliveny

Reputation: 2113

I think you just want this?

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)

Upvotes: 1

Deep
Deep

Reputation: 3202

Maybe this :

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) 

Note: if you want the maximum then use max() instead of sum().

Upvotes: 1

mucio
mucio

Reputation: 7137

If you use SUM you can aggregate the other two columns

  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

Upvotes: 1

Related Questions