Sql server查询根据不同条件从同一个表中选择结果

时间:2016-08-19 07:24:19

标签: sql-server join sql-server-2014

我有两张表,即Measurement and Clinical。

查询:

CREATE TABLE Measurement
(
    [MeasurementID] [int] IDENTITY(1,1) NOT NULL,
    [CHID] [int] NOT NULL,
    [MeasureID] [int] NOT NULL,
    [InputValue] [varchar](80) NOT NULL,
    [MeasurementDate] [datetime] NOT NULL
)

CREATE TABLE Clinical
(
    [ClinicalID] [int] IDENTITY(1,1) NOT NULL,
    [CHID] [int],
    [ClientID] [int] NOT NULL
)

insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(111,118,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(112,198,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(112,82,'Referred to Comm Pres',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(113,118,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(114,198,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(114,82,'Referred to Comm Pres',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(115,118,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(115,198,'No',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(115,82,'Referred to Comm Pres',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(116,118,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(116,198,'No',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(116,82,'Univeristy',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(117,188,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(117,198,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(117,82,'Univeristy',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(200,188,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(201,198,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(202,82,'Univeristy',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(203,188,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(203,198,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(205,188,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(203,5000,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(203,500,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(205,1555,'NO',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(207,188,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(208,198,'Yes',19-10-2016)
insert into Measurement(CHID,MeasureID,InputValue,MeasurementDate) values(209,82,'Referred to Comm Pres',19-10-2016)

insert into Clinical(CHID,ClientID) values(111,1)
insert into Clinical(CHID,ClientID) values(112,1)
insert into Clinical(CHID,ClientID) values(113,2)
insert into Clinical(CHID,ClientID) values(114,2)
insert into Clinical(CHID,ClientID) values(115,2)
insert into Clinical(CHID,ClientID) values(116,3)
insert into Clinical(CHID,ClientID) values(117,4)
insert into Clinical(CHID,ClientID) values(200,4)
insert into Clinical(CHID,ClientID) values(201,4)
insert into Clinical(CHID,ClientID) values(202,4)
insert into Clinical(CHID,ClientID) values(203,5)
insert into Clinical(CHID,ClientID) values(205,6)

insert into Clinical(CHID,ClientID) values(207,7)
insert into Clinical(CHID,ClientID) values(208,7)
insert into Clinical(CHID,ClientID) values(209,7)

我想要的结果如下:

ClientID | InputValue of(188) | InputValue of(198)| InputValue of(82)
----------------------------------------------------------------------    
7        | Yes                | Yes               | Referred to Comm Pres
6        | Yes                | null              | null
5        | yes                | yes               | null

注意:我们只应考虑具有输入值的188,198"是" 并且82值应该只是'提到Comm Pres'

任何人都可以帮助我。如果您需要更多说明,请发表评论。谢谢

我尝试了以下

with Table1 AS
(
   select 
       CH.ClientID, CM.*
   from 
       Measurement CM 
   inner join 
       Clinical CH ON CM.CHID = CH.CHID
   where 
       MeasureID in (188, 198, 82)
) 
select  
    T1.ClientID, T1.MeasurementID, T1.MeasureID, T1.CHID,
    T1.InputValue, T2.InputValue, T3.InputValue, T1.MeasurementDate
from  
    Table1 T1
join 
    Table1 T2 on (T1.ClientID = T2.ClientID) 
join 
    Table1 T3 on (T2.ClientID = T3.ClientID) 
where  
    T2.MeasureID = 198 
    and lower(T2.InputValue) = 'yes'
    and t3.MeasureID = 82 
    and T1.MeasureID = 188 
    and lower(T1.InputValue) = 'yes'
    and T1.ClientID not in (select ClientID 
                            from Table1 
                            where MeasureID = 82 
                              and InputValue not in ('Referred to Comm Pres'))
group by 
    T1.ClientID, T1.MeasurementID, T1.MeasureID, T1.CHID, 
    T1.InputValue, T2.InputValue, T3.InputValue, T1.MeasurementDate 
order by 
    T1.MeasurementDate, T1.MeasurementID, T1.CHID, T1.MeasureID 

1 个答案:

答案 0 :(得分:0)

尝试使用SQL Server PIVOT,如下所示。

SELECT  CLIENTID, 
        CASE WHEN [188] = 'YES' THEN [188] ELSE NULL END AS [InputValue of(188)] , 
        CASE WHEN [198] = 'YES' THEN [198] ELSE NULL END AS [InputValue of(198)],
        CASE WHEN [82] = 'Referred to Comm Pres' THEN [82] ELSE NULL END AS [InputValue of(82)]
FROM (
    SELECT B.ClientID,A.MEASUREID,A.InputValue,Row_Number() over(order by clientid) as ID -- Pivot grouping column
    FROM Measurement A
    INNER JOIN Clinical B
    ON A.CHID = B.CHID
    WHERE A.MEASUREID IN (188, 198, 82)
) as s
PIVOT
(
    MIN(InputValue)
    FOR MEASUREID IN ([188], [198], [82])
)AS pvt