2015-10-10 93 views
0

我在sql中有兩個表。第一個病人名單,第二個是他們的報告。所有患者的報告都在報告中,只要有身份證,我們就可以加入他們。每個病人都有一些報告(也許所有記錄的字段都沒有填寫)。現在我想做一份報告,獲取每位患者的最後一份報告,但如果該患者的最後一份記錄中某些字段爲空,則應填寫該患者記錄的最後填寫記錄。我在報告中有日期。SQL查詢加入和分組和

我想爲所有患者做。在這裏,我會爲一個病人增加一個PIC作爲一個例子

enter image description here

在上面的例子中,我想這個病人在報告中只是強調的。

我已經寫了這個查詢,但是它甚至在最後一條記錄中的一個字段爲空,而它在之前的記錄中有數據時也是這樣。

SELECT patient.bartar_id,patient.bartar_enteringthesystem,patient.bartar_proviencename, 
     patient.bartar_cityname,patient.bartar_coloplastrepname,patient.bartar_consultorname, 
     patient.bartar_provienceofsurgeryname,patient.bartar_cityofsurgeryname, 
     patient.bartar_surgeryhospitalname,patient.bartar_doctor,patient.bartar_patientstatusname, 
     patient.bartar_ostomytypename, patient.bartar_ostomytimename, 
     r.bartar_date,r.bartar_delay,r.bartar_nextcall,r.new_newcaller, 
     r.bartar_brandname,r.bartar_pastename,r.bartar_bagname,r.bartar_accname, 
     r.bartar_pastepermonth,r.bartar_bagepermonth,r.bartar_insuranceinfo, 
     patient.bartar_deathhealeddate,patient.bartar_dateofseurgery 
FROM [Bartar_MSCRM].[dbo].[Filteredbartar_newpaitient] as patient 
JOIN (SELECT r.*, row_number() over (partition by r.bartar_patientname 
            order by r.bartar_date desc) as seqnum 
     FROM [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] as r 
     where r.bartar_delay is not null 
    ) r 
ON r.bartar_patientname = patient.bartar_newpaitientid and seqnum = 1 
ORDER BY patient.bartar_id DESC ; 

病人表

enter image description here

報告表

enter image description here

加入

enter image description here

最終報告我想

enter image description here

+0

發佈一些示例數據並輸出,以便我們給您的答案可以在查詢中實現 – mohan111

回答

1

這是一個樣本, 你的情況,你必須得在子查詢中的每個列的值(無論是在連接語句,或在主SELECT語句 例如:

inner join (
     select distinct bartar_patientname 
       ,(select top 1 bartar_pastePerMonth from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_pastePerMonth is not null order by c2.bartar_date desc) as bartar_date 
       ,(select top 1 bartar_acc   from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_acc is not null order by c2.bartar_date desc) as bartar_acc 
       ,(select top 1 bartar_insuranceinfo from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_insuranceinfo is not null order by c2.bartar_date desc) as bartar_insuranceinfo 
       ,(select top 1 bartar_brand   from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] c2 where c2.bartar_patientname = cte.bartar_patientname and c2.bartar_brand is not null order by c2.bartar_date desc) as bartar_brand 
     from [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] cte 
     ) r 

同樣,這是解決方案的一個示例。

0

只是不加入爲每列什麼

JOIN (SELECT r.colx, row_number() over (partition by r.bartar_patientname 
            order by r.bartar_date desc) as seqnum 
     FROM [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] as r 
     where r.bartar_delay is not null and r.colx in not null 
    ) rx 
ON rx.bartar_patientname = patient.bartar_newpaitientid and seqnum = 1 
1

你的腳本是好的,因爲它看起來,所以我就地方上一個臨時表現在,並執行每個序列查詢並在之後通過「OR」進行過濾。

請嘗試下面的腳本。

SELECT patient.bartar_id,patient.bartar_enteringthesystem,patient.bartar_proviencename, 
      patient.bartar_cityname,patient.bartar_coloplastrepname,patient.bartar_consultorname, 
      patient.bartar_provienceofsurgeryname,patient.bartar_cityofsurgeryname, 
      patient.bartar_surgeryhospitalname,patient.bartar_doctor,patient.bartar_patientstatusname, 
      patient.bartar_ostomytypename, patient.bartar_ostomytimename, 
      r.bartar_date,r.bartar_delay,r.bartar_nextcall,r.new_newcaller, 
      r.bartar_brandname,r.bartar_pastename,r.bartar_bagname,r.bartar_accname, 
      r.bartar_pastepermonth,r.bartar_bagepermonth,r.bartar_insuranceinfo, 
      patient.bartar_deathhealeddate,patient.bartar_dateofseurgery 
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_pastepermonth ORDER BY r.bartar_date DESC) AS bartarpaste_sequence 
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_acc ORDER BY r.bartar_date DESC) AS bartaracc_sequence 
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_insuranceinfo ORDER BY r.bartar_date DESC) AS bartarins_sequence 
, ROW_NUMBER() OVER (PARTITION BY r.bartar_newpaitientid, r.bartar_brandname ORDER BY r.bartar_date DESC) AS bartarbrd_sequence 
    INTO #tmpPatientReport 
    FROM [Bartar_MSCRM].[dbo].[Filteredbartar_newpaitient] as patient 
    JOIN (SELECT r.*, row_number() over (partition by r.bartar_patientname 
             order by r.bartar_date desc) as seqnum 
      FROM [Bartar_MSCRM].[dbo].[Filteredbartar_callcenterreport] as r 
      where r.bartar_delay is not null 
     ) r 
    ON r.bartar_patientname = patient.bartar_newpaitientid and seqnum = 1 
    ORDER BY patient.bartar_id DESC; 

    SELECT 
     * 
    FROM #tmpPatientReport 
    WHERE 
     bartarpaste_sequence = 1 
     OR bartaracc_sequence = 1 
     OR bartarins_sequence = 1 
     OR bartarbrd_sequence = 1