我有兩個表,即測量和臨牀。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
請發表你嘗試過什麼到目前爲止.. – StackUser
我說什麼,我至今嘗試過 –