SQL按列计数

时间:2017-02-02 18:53:45

标签: mysql sql select

我的表

enter image description here

仅考虑黄线..

我的输出如下:

enter image description here

如何将其转换为:

enter image description here

我的查询:

NSObject

5 个答案:

答案 0 :(得分:1)

SELECT ot.PLAY_COUNT, COUNT(ot.CLIENT_ID) AS CLIENT_COUNT 
   FROM (SELECT 
            COUNT(DISTINCT(mt.SONG_ID)) AS PLAY_COUNT, 
            mt.CLIENT_ID 
         FROM `mytable` AS mt
         WHERE mt.PLAY_TS LIKE '%10/08/2016%' 
         GROUP BY mt.CLIENT_ID) AS ot 
   GROUP BY ot.PLAY_COUNT;

答案 1 :(得分:0)

将您的查询包装到另一个查询中:

select play_count as distinct_play_count, count(*) as client_count 
from (your_first_query) s1
group by play_count

答案 2 :(得分:0)

您需要向我们提供更多信息,但您可以这样做:

SELECT play_count, client_count FROM table
GROUP BY play_count

但你再次更好地改善问题的细节。

答案 3 :(得分:0)

SELECT PLAY_COUNT, COUNT(CLIENT_ID) AS CLIENT_COUNT
FROM Table_Name
GROUP BY PLAY_COUNT

答案 4 :(得分:0)

SELECT DISTINCT PLAY_COUNT AS DISTINCT_PLAY_COUNT, COUNT(CLIENT_ID) AS CLIENT_COUNT

FROM Table

GROUP BY PLAY_COUNT HAVING COUNT(CLIENT_ID) > 0