select
case
when s.X344731='Y' then 'Property Tax Assessment'
when s.X344732='Y' then 'Property Tax Payment'
when s.X344733='Y' then 'Birth and Death Certificate'
when s.X344734='Y' then ' Trade License'
when s.X344735='Y' then ' Complaints/Grievance Redressal'
when s.X344736='Y' then 'Water Connection'
when s.X344737='Y' then 'Sewerage Connection'
when s.X344738='Y' then 'Online Building Plan Approval System'
when s.X344739='Y' then ' Fire NOC'
when s.X3447310='Y' then ' NOC (Others)'
when s.X3447311='Y' then 'Certificates (Others)'
when s.X3447312='Y' then 'User Charges (water charges, parking fee, advertisement charges, electricity charges etc. )'
when s.X3447313='Y' then 'All of the above'
when s.X3447314='Y' then 'None of the above'
else 'Not Answered' end as municipal_services_do_you_use
from lime_survey_988773 s
left join lime_tokens_988773 t
on (s.token=t.token)
where s.submitdate is not null
and user_id=143561;
我想将这三个"财产税评估","财产税支付"和"出生和死亡证明"的值连接起来,它将如下所示:
User_id Municipal_Services_Do_you_use State
142351 ('Property Tax Assessment', 'Property Tax Payment','Birth and Death Certificate') Delhi
您可以将条件聚合与串联运算符(||
)
SELECT user_id AS "User_id",
MAX(CASE WHEN s.X344731='Y' THEN '(''Property Tax Assessment''' END)||
MAX(CASE WHEN s.X344732='Y' THEN ',''Property Tax Payment''' END)||
MAX(CASE WHEN s.X344732='Y' THEN ',''Birth and Death Certificate'')' END)
AS "Municipal_Services_Do_you_use"
FROM lime_survey_988773 s
LEFT JOIN lime_tokens_988773 t
ON t.token = s.token
WHERE user_id = 143561
GROUP BY user_id
将返回所需的结果,前提是在联接这些表后user_id = 143561
,每列至少返回一个名称为X344...
的'Y'
值。