简体   繁体   English

SQLite:group_concat 似乎只拉第一行?

[英]SQLite: group_concat only seems to pull first row?

I have a base query which functions as desired, and tells me which people have how many items for thing 'a':我有一个基本查询,它可以根据需要运行,并告诉我哪些人拥有事物“a”的数量:

SELECT Person || ' ' || COUNT(ResponsibleThing) FROM table WHERE thing='a' GROUP BY Person;

Person || ' ' || COUNT(ResponsibleThing)
-----------------------------------------------
Joe     340
Chris   34
Dave    612
Fred    272

What I really want is to collapse this result set into a CSV so that I can pivot on another column, with a result set that looks something like:我真正想要的是将此结果集折叠为 CSV,以便我可以在另一列上进行透视,结果集如下所示:

thing Person || '   ' || COUNT(ResponsibleThing)
----- -----------------------------------------
a     Joe     340,Chris     34,Dave     612,Fred    272
b     Chris     100, Frank     34

It seemed like group_concat would be the way to go, but strangely, I'm only getting back the first entry when I try to adapt the above query: SELECT group_concat((SELECT Person || ' ' || COUNT(ResponsibleThing) FROM table WHERE thing='a' GROUP BY Person));似乎group_concat将是要走的路,但奇怪的是,当我尝试调整上述查询时,我只取回第一个条目: SELECT group_concat((SELECT Person || ' ' || COUNT(ResponsibleThing) FROM table WHERE thing='a' GROUP BY Person));

group_concat((SELECT Person || '  ' || COUNT(ResponsibleThing) FROM table WHERE thing='a' GROUP BY Person))
----------------------------------------------------------------------------------------------------
Joe     340

Any ideas on how to get the full result set to be concatenated?关于如何连接完整结果集的任何想法? Many thanks.非常感谢。

You need two levels of aggregation: first by person (that's your existing query), then overall:您需要两个级别的聚合:首先是按个人(这是您现有的查询),然后是整体:

select group_concat(res) res
from (
    select person || ' ' || count(*) res
    from mytable
    where thing = 'a'
    group by person
) t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM