SQLite:group_concat 似乎只拉第一行?
SQLite: group_concat only seems to pull first row?
我有一个基本查询,它可以按需要运行,并告诉我哪些人有多少物品 'a':
SELECT Person || ' ' || COUNT(ResponsibleThing) FROM table WHERE thing='a' GROUP BY Person;
Person || ' ' || COUNT(ResponsibleThing)
-----------------------------------------------
Joe 340
Chris 34
Dave 612
Fred 272
我真正想要的是将这个结果集折叠成一个 CSV,这样我就可以在另一列上旋转,结果集看起来像:
thing Person || ' ' || COUNT(ResponsibleThing)
----- -----------------------------------------
a Joe 340,Chris 34,Dave 612,Fred 272
b Chris 100, Frank 34
似乎 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
关于如何连接完整结果集的任何想法?非常感谢。
您需要两个级别的聚合:首先按人员(这是您现有的查询),然后总体:
select group_concat(res) res
from (
select person || ' ' || count(*) res
from mytable
where thing = 'a'
group by person
) t
我有一个基本查询,它可以按需要运行,并告诉我哪些人有多少物品 'a':
SELECT Person || ' ' || COUNT(ResponsibleThing) FROM table WHERE thing='a' GROUP BY Person;
Person || ' ' || COUNT(ResponsibleThing)
-----------------------------------------------
Joe 340
Chris 34
Dave 612
Fred 272
我真正想要的是将这个结果集折叠成一个 CSV,这样我就可以在另一列上旋转,结果集看起来像:
thing Person || ' ' || COUNT(ResponsibleThing)
----- -----------------------------------------
a Joe 340,Chris 34,Dave 612,Fred 272
b Chris 100, Frank 34
似乎 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
关于如何连接完整结果集的任何想法?非常感谢。
您需要两个级别的聚合:首先按人员(这是您现有的查询),然后总体:
select group_concat(res) res
from (
select person || ' ' || count(*) res
from mytable
where thing = 'a'
group by person
) t