2017-07-25 53 views
-3

我想獲得person_discharge_date上具有NULL值的人員列表,但我還需要考慮create_date(獲取其最新值),以便該人員的姓名不會顯示兩次。獲得結果爲空不包含

對於我的問題,你怎麼也查詢它將只顯示person_discharge_date空值也基於最新的create_date?

我無法只顯示在person_discharge_date上有空的值,反之亦然。

我進表

IntakeTable 
    intake_id| person_id |person_discharge_date |create_date 
    ---------|------------|------------------------|----------------------- 
    2263  | 2289  |2010-06-28 00:00:00.000 |2012-04-17 13:41:54.503 
    2264  | 2289  |2010-08-28 00:00:00.000 |2012-04-17 13:41:54.547 
    4465  | 4489  |2011-02-12 00:00:00.000 |2012-04-17 13:41:54.563 
    1267  | 1218  |2009-12-11 00:00:00.000 |2012-04-17 13:41:54.707 
    7373  | 2348  |2010-06-03 00:00:00.000 |2012-04-17 13:41:55.297 
    4463  | 4429  |2011-09-20 00:00:00.000 |2012-04-17 13:41:57.133 
    6164  | 6129  |2011-05-18 00:00:00.000 |2012-04-17 13:41:57.143 
    12377 | 1888  |NULL     |2013-01-05 22:12:41.990 
    12378 | 5899  |NULL     |2013-01-05 22:17:02.957 
    12379 | 5899  |NULL     |2013-01-05 22:18:02.957 

這是我一個人的表

PersonTable 
person_id| fname  | 
---------|------------| 
2289  | marge  | 
4489  | shell  | 
1218  | linkoln | 
2348  | abe  | 
4429  | mark  | 
6129  | doom  | 
1888  | lorde  | 
5899  | braddy | 

這是我的查詢,但也有一些是與此失蹤,因爲我需要獲得最新的創建日期的人,以便該人不會從結果中顯示兩次。

select intake_id, person_id, person_discharge_date from intaketable it inner join persontable pt it.person_id = pt.person_id WHERE person_discharge_date IS NOT NULL 
+1

提示:'IS NULL','IS NOT NULL'?也許你應該展示你想要的結果和你嘗試過的任何查詢。 –

+0

解釋你有什麼「麻煩」。 –

+0

我不知道如何查詢它將只顯示person_discharge_date上有空值的人,反之亦然(顯示結果不包括空值) – Shulz

回答

0

你怎麼做一個查詢在那裏將只包括從person_discharge_date的 結果

檢查IS NULLWHERE條件所涉及的上述列說

的空
WHERE person_discharge_date IS NULL 

您可以使用group by並獲得像max()一樣

select person.*, xxx.person_discharge_date 
from person p join (
select person_id, person_discharge_date, 
    max(create_date) as latest 
from Intake 
where person_discharge_date is null 
group by person_id) xxx on p.person_id = xxx.person_id; 
+0

但我還需要獲取person_id的最新create_date,以便它不會顯示兩次相同的名稱。 – Shulz

+1

@Shulz,如果幫助 – Rahul

+0

在'group by'中未包含'person_discharge_date',那麼請參閱編輯答案。 – Shulz

0

我想我已經找到了解決這個與拉胡爾兩個代碼混合的幫助和其他網站

SELECT pi.person_id, 
     pi.intake_id, 
     pi.person_discharge_date, 
     pi.create_date 
    FROM intake pi INNER JOIN (select patient_id, MAX(create_date) 
    as LatestDate FROM intake group by patient_id) 
    pati ON pi.person_id = pati.person_id AND pi.Create_DATE = 
    pati.latestdate 
    WHERE person_discharge_date IS NULL