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

将返回所需的结果,前提是在连接这些表之后,名称为X344...的每列至少返回一个'Y'值,而名称为user_id = 143561.

Sql相关问答推荐

在数据分区内执行确定

对于表A中的每一行,更新表B中与金额有关的行

基于前面行的值:当x&>2时重复1,当连续3行x=0时则重复0

使用占位符向SQL INSERT查询添加 case

SQL -滞后于上一个非重复值

每组显示一行(表1中的分组值),表2中的不同列表用逗号分隔

在一个子查询中签入ID';S,如果未返回,则签入另一个子查询

将JSON文件导入Postgres 16数据库时出错(22P04上次预期列之后的额外数据)

SQL数据库规范化与数据插入

如何为缺少的类别添加行

在 PostgreSQL 中使用 ltree 列进行累积

如何使用最后一个非 NULL 值在 PostgreSQL 列中填充 NULL 值

Postgresql:在链接表中判断相关表中插入值的条件

如何根据 SQL Server 中 1 条语句中 SELECT 的结果进行 INSERT 或 UPDATE

String_Split 多列

我如何编写一个遍历数组数组并将所有值连接成一个字符串的 postgres 函数

MIN MAX 值与条件绑定

SQL Server Where 条件

如果当前日期是一周中的某一天,则从另一天提取结果

REGEXP 用于字符串格式化以对用空格分隔的字符和数字进行分组