Sql 将三个查询合并为一个结果

Sql 将三个查询合并为一个结果,sql,sql-server,r,Sql,Sql Server,R,我对SQL比较陌生,我正在努力提高我的工作流程的效率 特别是,我目前正在通过三个类似于R中的查询从MS SQL数据库中提取数据 select ID, 'active' as Indicator_1 from table1 where STATUS = '2' and type = '1' 这将返回这样一个表: ID Indicator_1 01 active 02 active 03 active ... ID Indicator_2 01 active 0

我对SQL比较陌生,我正在努力提高我的工作流程的效率

特别是,我目前正在通过三个类似于R中的查询从MS SQL数据库中提取数据

select ID, 'active' as Indicator_1 from table1
where STATUS = '2' and type = '1'
这将返回这样一个表:

ID  Indicator_1
01    active
02    active
03    active
   ...
ID  Indicator_2
01    active
03    active
04    active
   ...

select ID, 'active' as Indicator_1 from table1
where STATUS = '2' and type = '20'
ID  Indicator_3
01    NA/Blank
03    active
04    active
   ...
然后,我使用不同的类型值拉取相同的查询

这将返回这样一个表:

ID  Indicator_1
01    active
02    active
03    active
   ...
ID  Indicator_2
01    active
03    active
04    active
   ...

select ID, 'active' as Indicator_1 from table1
where STATUS = '2' and type = '20'
ID  Indicator_3
01    NA/Blank
03    active
04    active
   ...
这将返回这样一个表:

ID  Indicator_1
01    active
02    active
03    active
   ...
ID  Indicator_2
01    active
03    active
04    active
   ...

select ID, 'active' as Indicator_1 from table1
where STATUS = '2' and type = '20'
ID  Indicator_3
01    NA/Blank
03    active
04    active
   ...
然后,我将把R中的数据作为数据帧加载,并连接表

我知道这是低效的。 我想做的是,预期结果是询问一个返回组合结果的查询

ID  Indicator_1   Indicator_2  Indicator_3
01    active        active       NA/Blank
03    active       NA/Blank      active
04    Na/Blank      active       active
   ...
有什么建议吗

非常感谢

试试这个

;WITH CTE
AS
(
    SELECT 
       ID, 
       Indicator_1 = CASE [Type]
                       WHEN 1 THEN 'Active'
                    ELSE NULL END,
       Indicator_2 = CASE [Type]
                       WHEN 50 THEN 'Active'
                    ELSE NULL END
       FROM dbo.table1
          WHERE [status] = '2'
             AND [Type] IN ('1','50')
)
SELECT
    Id,
    Indicator_1 = COALESCE(MAX(Indicator_1),'NA/Blank'),
    Indicator_2 = COALESCE(MAX(Indicator_2),'NA/Blank')
    FROM CTE
       GROUP BY Id
试试这个查询! 使用UNION子句简单地组合查询

    SELECT  
           ID                     ,
          'active' AS Indicator_1 ,
           null    AS Indicator_2 , 
           null    AS Indicator_3 
    FROM   table1
    WHERE  STATUS = '2' 
    AND    type = '1'

    UNION

    SELECT 
           ID                     ,
           null    AS Indicator_1 , 
          'active' AS Indicator_2 ,
           null    AS Indicator_3 
    FROM   table1
    WHERE  STATUS = '2' 
    AND    type = '50'

    UNION

    SELECT 
           ID                     ,
           null    AS Indicator_1 ,
           null    AS Indicator_2 , 
          'active' AS Indicator_3 
    FROM   table1
    WHERE  STATUS = '2'  
    AND    type = '20'

您还可以在SQL中执行简单的查询,然后在R中修改data.frame以获得所需的结果

轻松查询:

select ID, 'active','type' as Indicator_1 from table1
where STATUS = '2' and type in ('1','50','20')
R中的结果类似于db data.frame,如下所示:

ID<-c(1,2,3,1,5,2)
Indicator<-c("active","active","active","active","active","active")
Status<-c(1,1,50,50,50,20) db<-data.frame(cbind(ID,Indicator,Status))

  ID Indicator Status
  1    active      1
  2    active      1
  3    active     50
  1    active     50
  5    active     50
  2    active     20
现在您可以处理data.frame

db_merge<-merge(x=db[db$Status==1,],y=db[db$Status==50,],by="ID",all=T)
db_merge<-merge(x=db_merge,y=db[db$Status==20,],by="ID",all=T)
您的结果:

ID Indicator.x Status.x Indicator.y Status.y Indicator Status
1      active        1      active       50      <NA>   <NA>
2      active        1        <NA>     <NA>    active     20
3        <NA>     <NA>      active       50      <NA>   <NA>
5        <NA>     <NA>      active       50      <NA>   <NA>

添加所有3个查询、结果和预期结果。键入“1”、“50”就足够了-然后可以在R中重塑数据。如果坚持在SQL中执行该部分,例如,如果结果不适合RAM,则可以使用外部联接。我用更多信息编辑了该问题。希望它更精确。我不明白如何从表1中选择ID“active”作为指示符_1,其中STATUS='2'和type='20'在SQL Server的第二列中返回一个字符串Na/Blank作为结果值?@VojtěchDohnal,因为结果仅用于说明。这不是实际的结果,我已经在做了。但是我相信在SQL中正确指定查询是更好的方法。我同意你的观点!这只是另一种效率较低的方法。