SQL 具有多个条件的 Server 2014 查询
SQL Server 2014 query with multiple conditions
我有一个具有多个条件的 SQL 查询,对于给定的源(下面列出)我需要将结果集设置为具有以下条件的最终(下面列出):
- 条件01:对于活动站点,应以“RO”作为联系人角色进行联系
- 条件 02:对于非活动站点,应以“所有者”作为联系人角色联系
- 条件03:如果活动站点没有以“RO”作为联系人角色的联系人,则应以'Owner'作为联系人角色进行联系人
- 条件 04:如果非活动站点没有以“所有者”作为联系人角色进行联系,则应以 'RO' 作为联系人角色进行联系
条件 05:如果活动 site/inactive 没有与“RO”或“所有者”作为联系人角色联系,则应与“操作员”作为联系人角色联系
条件 06:仅适用于活动站点 避免 'XYZ' 联系 'RO' 作为联系角色并选择另一个联系 'RO' 作为联系角色,如果站点没有 'RO' 然后选择 'Owner' 或 'operator' 作为联系人角色
条件 07:如果网站 (active/inactive) 没有任何联系人,那么这些网站的联系人字段中应该有 'NULL' 值。
原始数据集记录超过 20K,我们能否将上述所有条件包含在一个查询中(如果可能,不使用子查询)?
Source
======
Site_Status Site_id Site_Contact Contact Role
Active 123 Lilly Owner
Active 123 Elan RO
Inactive 345 Rose Owner
Inactive 345 Jack RO
Active 678 Robert Owner
Inactive 912 Linda RO
Active 234 Nike Operator
Inactive 456 Frank Operator
Active 808 XYZ RO
Active 808 Kelly Owner
Active 999 XYZ RO
Active 999 Debbi Operator
Active 122
Inactive 188
Final
======
Site_Status Site_id Site_Contact ContactRole
Active 123 Elan RO
Inactive 345 Rose Owner
Active 678 Robert Owner
Inactive 912 Linda RO
Active 234 Nick Operator
Inactive 456 Frank Operator
Active 808 Kelly Owner
Active 999 Debbi Operator
Active 122 NULL NULL
Inactive 188 NULL NULL
提前致谢!
类似这样...使用 window 函数和条件排序。
declare @YourTable table (Site_Status varchar(64), Site_id int, Site_Contact varchar(64), ContactRole varchar(64))
insert into @YourTable
values
('Active',123,'Lilly','Owner'),
('Active',123,'Elan','RO'),
('Inactive',345,'Rose','Owner'),
('Inactive',345,'Jack','RO'),
('Active',678,'Robert','Owner'),
('Inactive',912,'Linda','RO'),
('Active',234,'Nick','Operator'),
('Inactive',456,'Frank','Operator')
select
t.*
from @YourTable t
inner join
(select
Site_id
,Site_Status
,ContactRole
,Active = row_number() over (partition by Site_id, Site_Status order by case
when Site_Status = 'Active' and ContactRole = 'RO' then 1
when Site_Status = 'Active' and ContactRole = 'Owner' then 2
when Site_Status = 'Active' and ContactRole = 'Operator' then 3
end)
,InActive = row_number() over (partition by Site_id, Site_Status order by case
when Site_Status = 'InActive' and ContactRole = 'Owner' then 1
when Site_Status = 'InActive' and ContactRole = 'RO' then 2
when Site_Status = 'InActive' and ContactRole = 'Operator' then 3
end)
from @YourTable) x on
x.Site_id = t.Site_id
and x.Site_Status = t.Site_Status
and t.ContactRole = x.ContactRole
and Active = 1
and InActive = 1
您可以使用 table 变量并逐渐填充它,但它不会是单个查询(它是存储过程的 ggod 候选者)。可以使用 union(相当大的一个)创建带有子查询的单个查询:
select * from [Source]
where [Contact Role] = 'RO' and Site_Status = 'Active'
union
select * from [Source]
where [Contact Role] = 'Owner' and Site_Status = 'Inactive'
union
select * from [Source] s1
where [Contact Role] = 'Owner' and Site_Status = 'Active'
and not exists(
select 1 from [Source] s2 where s1.Site_Id = s2.Site_id
and s2.Site_Status = 'Active' and s2.[Contact Role] = 'RO'
)
union
select * from [Source] s1
where [Contact Role] = 'RO' and Site_Status = 'Inactive'
and not exists(
select 1 from [Source] s2 where s1.Site_Id = s2.Site_id
and s2.Site_Status = 'Inactive' and s2.[Contact Role] = 'Owner'
)
union
select * from [Source] s1
where [Contact Role] = 'Operator'
and not exists(
select 1 from [Source] s2 where s1.Site_Id = s2.Site_id
and (s2.[Contact Role] = 'Owner' or s2.[Contact Role] = 'RO')
)
我有一个具有多个条件的 SQL 查询,对于给定的源(下面列出)我需要将结果集设置为具有以下条件的最终(下面列出):
- 条件01:对于活动站点,应以“RO”作为联系人角色进行联系
- 条件 02:对于非活动站点,应以“所有者”作为联系人角色联系
- 条件03:如果活动站点没有以“RO”作为联系人角色的联系人,则应以'Owner'作为联系人角色进行联系人
- 条件 04:如果非活动站点没有以“所有者”作为联系人角色进行联系,则应以 'RO' 作为联系人角色进行联系
条件 05:如果活动 site/inactive 没有与“RO”或“所有者”作为联系人角色联系,则应与“操作员”作为联系人角色联系
条件 06:仅适用于活动站点 避免 'XYZ' 联系 'RO' 作为联系角色并选择另一个联系 'RO' 作为联系角色,如果站点没有 'RO' 然后选择 'Owner' 或 'operator' 作为联系人角色
条件 07:如果网站 (active/inactive) 没有任何联系人,那么这些网站的联系人字段中应该有 'NULL' 值。
原始数据集记录超过 20K,我们能否将上述所有条件包含在一个查询中(如果可能,不使用子查询)?
Source
======
Site_Status Site_id Site_Contact Contact Role
Active 123 Lilly Owner
Active 123 Elan RO
Inactive 345 Rose Owner
Inactive 345 Jack RO
Active 678 Robert Owner
Inactive 912 Linda RO
Active 234 Nike Operator
Inactive 456 Frank Operator
Active 808 XYZ RO
Active 808 Kelly Owner
Active 999 XYZ RO
Active 999 Debbi Operator
Active 122
Inactive 188
Final
======
Site_Status Site_id Site_Contact ContactRole
Active 123 Elan RO
Inactive 345 Rose Owner
Active 678 Robert Owner
Inactive 912 Linda RO
Active 234 Nick Operator
Inactive 456 Frank Operator
Active 808 Kelly Owner
Active 999 Debbi Operator
Active 122 NULL NULL
Inactive 188 NULL NULL
提前致谢!
类似这样...使用 window 函数和条件排序。
declare @YourTable table (Site_Status varchar(64), Site_id int, Site_Contact varchar(64), ContactRole varchar(64))
insert into @YourTable
values
('Active',123,'Lilly','Owner'),
('Active',123,'Elan','RO'),
('Inactive',345,'Rose','Owner'),
('Inactive',345,'Jack','RO'),
('Active',678,'Robert','Owner'),
('Inactive',912,'Linda','RO'),
('Active',234,'Nick','Operator'),
('Inactive',456,'Frank','Operator')
select
t.*
from @YourTable t
inner join
(select
Site_id
,Site_Status
,ContactRole
,Active = row_number() over (partition by Site_id, Site_Status order by case
when Site_Status = 'Active' and ContactRole = 'RO' then 1
when Site_Status = 'Active' and ContactRole = 'Owner' then 2
when Site_Status = 'Active' and ContactRole = 'Operator' then 3
end)
,InActive = row_number() over (partition by Site_id, Site_Status order by case
when Site_Status = 'InActive' and ContactRole = 'Owner' then 1
when Site_Status = 'InActive' and ContactRole = 'RO' then 2
when Site_Status = 'InActive' and ContactRole = 'Operator' then 3
end)
from @YourTable) x on
x.Site_id = t.Site_id
and x.Site_Status = t.Site_Status
and t.ContactRole = x.ContactRole
and Active = 1
and InActive = 1
您可以使用 table 变量并逐渐填充它,但它不会是单个查询(它是存储过程的 ggod 候选者)。可以使用 union(相当大的一个)创建带有子查询的单个查询:
select * from [Source]
where [Contact Role] = 'RO' and Site_Status = 'Active'
union
select * from [Source]
where [Contact Role] = 'Owner' and Site_Status = 'Inactive'
union
select * from [Source] s1
where [Contact Role] = 'Owner' and Site_Status = 'Active'
and not exists(
select 1 from [Source] s2 where s1.Site_Id = s2.Site_id
and s2.Site_Status = 'Active' and s2.[Contact Role] = 'RO'
)
union
select * from [Source] s1
where [Contact Role] = 'RO' and Site_Status = 'Inactive'
and not exists(
select 1 from [Source] s2 where s1.Site_Id = s2.Site_id
and s2.Site_Status = 'Inactive' and s2.[Contact Role] = 'Owner'
)
union
select * from [Source] s1
where [Contact Role] = 'Operator'
and not exists(
select 1 from [Source] s2 where s1.Site_Id = s2.Site_id
and (s2.[Contact Role] = 'Owner' or s2.[Contact Role] = 'RO')
)