具有多个条件的SQL Server 2014查询

时间:2018-05-23 20:46:07

标签: sql-server sql-server-2014

我有一个具有多个条件的SQL查询,对于给定的Source(下面列出)我需要将结果集设置为Final(下面列出),条件如下:

  • 条件01:对于活动站点,请联系“RO”作为联系角色
  • 条件02:对于非活动网站,请联系“所有者”作为联系人角色
  • 条件03:如果活动网站没有与“RO”联系作为联系人角色,则应与“所有者”联系作为联系人角色
  • 条件04:如果非活动网站没有与“所有者”联系作为联系人角色,则应与“RO”联系作为联系人角色
  • 条件05:如果活动网站/非活动状态与“RO”或“所有者”没有联系作为联系人角色,则应与“运营商”联系作为联系人角色

  • 条件06:仅针对活动网站,避免“XYZ”与“RO”联系作为联系人角色,并选择另一个联系人与“RO”作为联系角色,如果该网站没有“RO”,则选择“所有者'或'运营商'作为联系人角色

  • 条件07:如果站点(活动/非活动)没有任何联系人,那么这些站点在联系人字段中应该具有“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

提前致谢!

1 个答案:

答案 0 :(得分:1)

像这样......使用窗口函数和条件排序。

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