gpt4 book ai didi

调查结果的 SQL 问题

转载 作者:行者123 更新时间:2023-12-03 02:10:40 25 4
gpt4 key购买 nike

您好,我有一个查询返回此

member_id   question_variable                                  response_id label
----------- -------------------------------------------------- ----------- ----------------------------
35 area 15 Sydney (Metro)
35 relationship_status 8 Single
35 education 31 Bachelor Degree
35 house_hold_income 4 $75,001 to $100,000
35 pets 36 Dog
35 pets 37 Fish

如何检测重复的结果,例如宠物,response_id = 36,37 且标签 = Dog、Fish像这样

member_id   question_variable                                  response_id label
----------- -------------------------------------------------- ----------- ----------------------------
35 area 15 Sydney (Metro)
35 relationship_status 8 Single
35 education 31 Bachelor Degree
35 house_hold_income 4 $75,001 to $100,000
35 pets 36,37 Dog,Fish

最佳答案

您必须使用关键字STUFF才能获得上述结果。

查询:

SELECT DISTINCT T1.MEMBER_ID,T1.QUESTION_VARIABLE,
STUFF((SELECT DISTINCT ',' + T2.RESPONSE_ID
FROM TEST T2
WHERE T1.QUESTION_VARIABLE = T2.QUESTION_VARIABLE
FOR XML PATH('') ),1,1,'') AS RESPONSE_ID,
STUFF((SELECT DISTINCT ',' + T2.LABEL
FROM TEST T2
WHERE T1.QUESTION_VARIABLE = T2.QUESTION_VARIABLE
FOR XML PATH('') ),1,1,'') AS LABEL

FROM TEST T1
;

HERE IS THE LINK TO SQL FIDDLE http://sqlfiddle.com/#!3/64515/3

关于调查结果的 SQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23994337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com