2016-08-19 177 views
0

我有兩個表,即測量和臨牀。Sql server查詢根據不同的條件從同一表中選擇結果

查詢:

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 

注意:我們應該只考慮188198具有輸入值 「是」 和82值應該只有「提到Comm Pres'

任何人都可以請幫我這個。如果您需要更多clarification.Thank你

我想請評論以下

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 
+0

請發表你嘗試過什麼到目前爲止.. – StackUser

+0

我說什麼,我至今嘗試過 –

回答

0

嘗試使用SQL服務器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 
+0

以上答案有些工作,但它也呈現出客戶端,它具有82值=「大學」,這不應該顯示如果客戶端有多個條目,那麼它應顯示爲單獨的行 –

+0

@ adi_2010我已修改答案以將所有客戶端顯示爲單獨的行。我完全不理解你對「大學」過濾器的評論,但我想這應該是微不足道的,你應該能夠自己改變查詢。 – Muthukumar

相關問題