SQL 标记 - 在结果中返回新列

SQL tagging - Returning new column in results

我需要为标记为 Tags

的查询创建一个新列

一共有三个标签,定义如下:

地铁:城市 = 芝加哥

邮寄:ACNT = 'ACT'

问候语:在 ('Ms.','Mrs.')

中的称呼

当前 table:

 ID    Salutation    City          State    ACNT 
 01    Ms.           Delray Beach  FL                   
 02    Mrs.          Lauderhill    FL       DCT
 03    Ms.           New York      NY    
 04    Ms.           Chicago       IL       ACT
 05                  Chicago       IL       ACT

我需要在我的输出中添加一列标签,就像这样。

 ID    Salutation    City          State    ACNT   Tags
 01    Ms.           Delray Beach  FL              Greeting     
 02    Mrs.          Lauderhill    FL       DCT    Greeting
 03    Ms.           New York      NY              Greeting
 04    Ms.           Chicago       IL       ACT    Metro, Greeting, Mailing
 05                  Chicago       IL       ACT    Metro, Mailing

我以前用过Stuff,但不是这种方式。任何 help/guidance 将不胜感激。

如果您使用的是最新版本的 Oracle,则可以使用派生的 table 标签交叉应用(使用 case 表达式,正如@serfe 建议的那样):

select id, salutation, city, state, acnt, tag
from your_table
cross apply (
  select case when city = 'Chicago' then 'Metro' end as tag from dual
  union all
  select case when salutation in ('Ms.', 'Mrs.') then 'Greeting' end as tag from dual
  union all
  select case when acnt = 'ACT' then 'Mailing' end as tag from dual
)

然后使用 listagg() 以您想要的形式获取列表:

select id, salutation, city, state, acnt,
  listagg (tag, ', ') within group (order by null) as tags
from your_table
cross apply (
  select case when city = 'Chicago' then 'Metro' end as tag from dual
  union all
  select case when salutation in ('Ms.', 'Mrs.') then 'Greeting' end as tag from dual
  union all
  select case when acnt = 'ACT' then 'Mailing' end as tag from dual
)
group by id, salutation, city, state, acnt
ID SALUTATION CITY STATE ACNT TAGS
03 Ms. New York NY null Greeting
01 Ms. Delray Beach FL null Greeting
02 Mrs. Lauderhill FL DCT Greeting
04 Ms. Chicago IL ACT Metro, Greeting, Mailing
05 null Chicago IL ACT Metro, Mailing

db<>fiddle

TAGS 的表达式为:

 RTRIM(
   CASE WHEN salutation IN ('Ms.','Mrs.') THEN 'Greeting' || ', ' ELSE '' END ||
   CASE WHEN city = 'Chicago' THEN 'Metro' || ', ' END ||
   CASE WHEN acnt = 'ACT' THEN 'Mailing' END
 ,', ')

您可以在 UPDATE 语句中使用该表达式来设置新的 TAGS 列的值。或者,您可以将其放入视图或 SQL 查询或虚拟列中,以根据需要计算 TAGS 值。

如果您愿意接受标签外观的细微变化,使用 concat 使用 ||

会更容易
select t.*, case when city='Chicago' then '(Metro)' else '' end ||
            case when salutation in ('Ms.','Mrs.') then '(Greeting)' else '' end ||
            case when acnt = 'ACT' then '(Mailing)' else '' end as tags
from your_table t;

输出

ID SALUTATION CITY STATE ACNT TAGS
01 Ms. Delray Beach FL (Greeting)
02 Mrs. Lauderhill FL DCT (Greeting)
03 Ms. New York NY (Greeting)
04 Ms. Chicago IL ACT (Metro)(Greeting)(Mailing)
05 Chicago IL ACT (Metro)(Mailing)

对 Matthew 的出色解决方案稍作调整,您还可以执行以下操作以获得您想要的结果

select t.*, ltrim(case when city='Chicago' then 'Metro' else '' end ||
                  case when salutation in ('Ms.','Mrs.') then ',Greeting' else '' end ||
                  case when acnt = 'ACT' then ',Mailing' else '' end,',') as tags
from your_table t