[英]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 ![]() |
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![]() |
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 ![]() |
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) ![]() |
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.