Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 具有多个条件的SQL Server 2014查询_Sql Server_Sql Server 2014 - Fatal编程技术网

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

Sql server 具有多个条件的SQL Server 2014查询,sql-server,sql-server-2014,Sql Server,Sql Server 2014,我有一个带有多个条件的SQL查询,对于给定的源(如下所列),我需要使用以下条件将结果集获取为Final(如下所列): 条件01:对于活动现场,应与“RO”联系,作为联系人 条件02:对于非活动站点,应作为联系人角色与“所有者”联系 条件03:如果活动站点没有以联系人角色与“RO”联系,则应以联系人角色与“所有者”联系 条件04:如果非活动站点没有作为联系人角色与“所有者”联系,则应作为联系人角色与“RO”联系 条件05:如果活动站点/非活动站点没有作为联系人角色与“RO”或“所有者”联系,则应

我有一个带有多个条件的SQL查询,对于给定的源(如下所列),我需要使用以下条件将结果集获取为Final(如下所列):

  • 条件01:对于活动现场,应与“RO”联系,作为联系人
  • 条件02:对于非活动站点,应作为联系人角色与“所有者”联系
  • 条件03:如果活动站点没有以联系人角色与“RO”联系,则应以联系人角色与“所有者”联系
  • 条件04:如果非活动站点没有作为联系人角色与“所有者”联系,则应作为联系人角色与“RO”联系
  • 条件05:如果活动站点/非活动站点没有作为联系人角色与“RO”或“所有者”联系,则应作为联系人角色与“操作员”联系

  • 条件06:仅适用于活动站点,避免以“RO”作为联系人角色与“XYZ”联系,并选择另一个以“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

提前谢谢

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

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

您可以使用一个表变量并逐步填充它,但它不是一个查询(它是存储过程的ggod候选)。可以使用union(相当大的union)创建包含子查询的单个查询:


对使用
操作符。您当前的查询是什么样子的?您能否以可消费的格式而不是图像发布数据?为什么?这里是一个了解发布数据的更好方法的好地方。不要轻视@SeanLange的建议。从现在起,不要使用图片。。。使用DDL和我在回答中提供的样本数据。我是这个网站的新手,我正在学习如何正确发布它。感谢您提供有用的链接。下次我会记住的。非常感谢。这帮了大忙。@user119260完全不用担心使用您的逻辑与子查询相比,大大减少了查询的运行时间。我真的再次感谢你。但我还有两个条件(06和07)请求包含在查询中。您能帮助我如何将这些条件合并到您的相同逻辑中吗。@user119260最好打开一个新问题,因为它会比这个问题吸引更多的注意力,这个问题对于这个站点来说是老问题。我可以通过添加where子句作为where site_contact'XYZ'用于'x'表,将条件06包括在查询中。我的工作条件是07,如果我不能做到这一点,那么我会把它作为新的问题来回答。非常感谢!
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')
)