结合三个查询作为一个结果

时间:2018-01-15 09:51:43

标签: 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
   ...

然后,我使用不同类型值来提取相同的查询。

select ID, 'active' as Indicator_1 from table1
where STATUS = '2' and type = '50'

这将返回一个表:

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
   ...

有任何建议怎么做?

非常感谢。

3 个答案:

答案 0 :(得分:3)

试试这个

;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

答案 1 :(得分:1)

试试这个查询! 简单地使用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'

答案 2 :(得分:0)

您还可以在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>