我陷入了SQL查询中。我的结果如下表所示,最终结果必须在报告中显示如下:
id Question
-------------
13 ABC
13 ABC
13 QWE
13 ABC
13 QWE
13 ABC
预期结果:
id Result
--------------------
13 4 ABC, 2 QWE
有人可以帮我吗?谢谢。
答案 0 :(得分:0)
这需要预聚合和字符串聚合。
with t as (
select id, question, count(*) as cnt
from t
group by id
)
select i.id,
stuff( (select ', ' + convert(varchar(255), cnt) + ' ' + question
from t t2
where t2.id = i.id
for xml path ('')
), 1, 2, ''
) as result
from (select distinct id from t) i;
答案 1 :(得分:0)
def main():
for file in get_all_files(True):
parse_file(file)