简体   繁体   English

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

[英]SQL tagging - Returning new column in results

I need to create a new column for my query labeled Tags我需要为标记为Tags的查询创建一个新列

There are three tags and the definitions are below:共有三个标签,定义如下:

Metro: City = Chicago地铁:城市 = 芝加哥

Mailing: ACNT = 'ACT'邮寄:ACNT = 'ACT'

Greeting: Salutation in ('Ms.','Mrs.')问候语:在 ('Ms.','Mrs.') 中的称呼

Current 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

I need to add a column Tags to my output, like this.我需要像这样在我的 output 中添加一列标签。

 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

I have used Stuff before but not in this manner.我以前用过Stuff ,但不是以这种方式。 Any help/guidance would be appreciated.任何帮助/指导将不胜感激。

An expression for TAGS would be: 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
 ,', ')

You could use that expression in an UPDATE statement to set the value for a new TAGS column.您可以在UPDATE语句中使用该表达式来设置新TAGS列的值。 Or, you could put it in a view or SQL query or virtual column to compute the TAGS value as needed.或者,您可以将其放在视图或 SQL 查询或虚拟列中,以根据需要计算TAGS值。

You can do it with CASE - WHEN statement.您可以使用 CASE - WHEN 语句来做到这一点。 More info here: https://www.w3schools.com/sql/sql_case.asp更多信息在这里: https://www.w3schools.com/sql/sql_case.asp

If you're on a recent version of Oracle you can use cross-apply with a derived table of tags (using case expressions, as @serfe suggested):如果您使用的是 Oracle 的最新版本,则可以将 cross-apply 与派生的标签表一起使用(使用大小写表达式,如 @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 is not null then 'Greeting' end as tag from dual
  union all
  select case when acnt = 'ACT' then 'Mailing' end as tag from dual
)

and then use listag() to get the list in the form you want:然后使用listag()以您想要的形式获取列表:

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 is not null 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 ID SALUTATION称呼 CITY城市 STATE STATE ACNT碳纳米管 TAGS标签
03 03 Ms.小姐。 New York纽约 NY纽约 null null Greeting问候
01 01 Ms.小姐。 Delray Beach德拉海滩 FL佛罗里达州 null null Greeting问候
02 02 Mrs.太太。 Lauderhill劳德希尔 FL佛罗里达州 DCT DCT Greeting问候
04 04 Ms.小姐。 Chicago芝加哥 IL伊利诺伊州 ACT行为 Metro, Greeting, Mailing地铁, 问候语, 邮寄
05 05 null null Chicago芝加哥 IL伊利诺伊州 ACT行为 Metro, Mailing地铁,邮寄

db<>fiddle db<>小提琴

If you're open to a slight variation in how the tags looks, it can get easier with a concat using ||如果您对标签外观的细微变化持开放态度,使用||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;

Outputs输出

ID ID SALUTATION称呼 CITY城市 STATE STATE ACNT碳纳米管 TAGS标签
01 01 Ms.小姐。 Delray Beach德拉海滩 FL佛罗里达州 (Greeting) (问候)
02 02 Mrs.太太。 Lauderhill劳德希尔 FL佛罗里达州 DCT DCT (Greeting) (问候)
03 03 Ms.小姐。 New York纽约 NY纽约 (Greeting) (问候)
04 04 Ms.小姐。 Chicago芝加哥 IL伊利诺伊州 ACT行为 (Metro)(Greeting)(Mailing) (地铁)(问候)(邮寄)
05 05 Chicago芝加哥 IL伊利诺伊州 ACT行为 (Metro)(Mailing) (地铁)(邮寄)

With a slight tweak to Matthew's great solution, you can also do the following to get exactly what you want通过对 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM