如何根据另一列的值在 SQL select 查询中 create/add 作为 return 输出的列?
How to create/add a column as a return output in an SQL select query based on another column's values?
我使用 Case 语句将一列 (industry_type
) 动态添加到 SQL 视图。现在我想使用同一列 name/values 添加另一个具有 industry_group
输出的动态列。我尝试使用相同的方法创建 industry_group
列。但我无法这样做,因为在视图中创建的列在“main table”中不可用。
我是否需要在源中创建一个带有 industry_group
的新列?或者有没有办法在同一视图和 return industry_group
值中动态创建它?
如果你能帮助我解决这个问题,那将是一个很大的帮助。
示例代码:
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
from table
输出:
Date Industry Type
------------------------------
7/02/2021 Poultry facilities
7/02/2021 Poultry facilities
7/02/2021 Poultry facilities
10/02/2021 Poultry facilities
11/01/2021 Hospitals
10/01/2021 Hospitals
5/02/2021 Hospitals
试过这个脚本1:
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
'PF' as industry_group,
from table
输出:
Date Industry Type Industry Group
----------------------------------------------
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
10/02/2021 Poultry facilities PF
11/01/2021 Hospitals PF
10/01/2021 Hospitals PF
5/02/2021 Hospitals PF
试过脚本 2
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
end as [industry_type],
'PF' as industry_group,
case
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
'HOS' as industry_group,
from table
很遗憾,“脚本 2”无法运行。
期望的输出:
Date Industry Type Industry Group
---------------------------------------
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
10/02/2021 Poultry facilities PF
11/01/2021 Hospitals HOS
10/01/2021 Hospitals HOS
5/02/2021 Hospitals HOS
我需要根据 10 多个搜索条件return 正确的行业组
请注意:industry_type
在“master table”中不可用
谢谢大家!
您的输出似乎与查询不匹配:查询未显示任何列日期,我希望输出包含 3 列:
Column1, column2, industry_type
相反它只有 2
如果您粘贴实际代码而不是来自 excel
的图片,将会有所帮助
无论如何要在 select 中添加一列,只需将其添加到字段中:
SELECT column1,
column2,
case
...
END AS industry_type,
"PF" as industry_group
FROM table;
问题增强后更新
每一列本身就是一个规则,如果你想使它依赖于以前的你应该重写规则两次,或者进行嵌套查询
第一个例子
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'PF'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'HOS'
else null
end as [industry_group],
from table
第二个例子
Select
Date, industry_type,
Case
When industry_type = "Hospitals" then "HOS"
When industry_type = "Poultry facilities" then "POL"
...
End
As industry_group
FROM (
select
date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
end as [industry_type],
case
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
from table)
我使用 Case 语句将一列 (industry_type
) 动态添加到 SQL 视图。现在我想使用同一列 name/values 添加另一个具有 industry_group
输出的动态列。我尝试使用相同的方法创建 industry_group
列。但我无法这样做,因为在视图中创建的列在“main table”中不可用。
我是否需要在源中创建一个带有 industry_group
的新列?或者有没有办法在同一视图和 return industry_group
值中动态创建它?
如果你能帮助我解决这个问题,那将是一个很大的帮助。
示例代码:
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
from table
输出:
Date Industry Type
------------------------------
7/02/2021 Poultry facilities
7/02/2021 Poultry facilities
7/02/2021 Poultry facilities
10/02/2021 Poultry facilities
11/01/2021 Hospitals
10/01/2021 Hospitals
5/02/2021 Hospitals
试过这个脚本1:
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
'PF' as industry_group,
from table
输出:
Date Industry Type Industry Group
----------------------------------------------
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
10/02/2021 Poultry facilities PF
11/01/2021 Hospitals PF
10/01/2021 Hospitals PF
5/02/2021 Hospitals PF
试过脚本 2
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
end as [industry_type],
'PF' as industry_group,
case
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
'HOS' as industry_group,
from table
很遗憾,“脚本 2”无法运行。
期望的输出:
Date Industry Type Industry Group
---------------------------------------
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
7/02/2021 Poultry facilities PF
10/02/2021 Poultry facilities PF
11/01/2021 Hospitals HOS
10/01/2021 Hospitals HOS
5/02/2021 Hospitals HOS
我需要根据 10 多个搜索条件return 正确的行业组
请注意:industry_type
在“master table”中不可用
谢谢大家!
您的输出似乎与查询不匹配:查询未显示任何列日期,我希望输出包含 3 列:
Column1, column2, industry_type
相反它只有 2
如果您粘贴实际代码而不是来自 excel
的图片,将会有所帮助无论如何要在 select 中添加一列,只需将其添加到字段中:
SELECT column1,
column2,
case
...
END AS industry_type,
"PF" as industry_group
FROM table;
问题增强后更新
每一列本身就是一个规则,如果你想使它依赖于以前的你应该重写规则两次,或者进行嵌套查询
第一个例子
select
column 1, column 2, date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'PF'
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'HOS'
else null
end as [industry_group],
from table
第二个例子
Select
Date, industry_type,
Case
When industry_type = "Hospitals" then "HOS"
When industry_type = "Poultry facilities" then "POL"
...
End
As industry_group
FROM (
select
date,
case
when (column 1 like '%12321%' or column 2 like'%poultry%'
then 'Poultry Facilities'
end as [industry_type],
case
when (column 1 like '%43434%') or column 2 like'%Hospi%'
then 'Hospitals'
else null
end as [industry_type],
from table)