2014-05-22 74 views
1

我有兩個表th_Therapy_Noteth_Approved。當th_Therapy_Note中的筆記獲得批准時,應用程序將向th_Approved插入一條記錄。如何從表中選擇多行存在的單行

由於以下幾個原因,筆記可能會被拒絕(不要問我爲什麼,因爲我沒有設計這個程序,哈哈)。因此,如果一張紙幣被批准後被拒絕,則插入另一個條目th_Approved

th_Approved.th_approved_isApproved是一個布爾(位)列,因此視狀態,在真或假

那麼多行的同一個音符此列在這個表中的音符輸入可以th_Approved可能存在不同th_Approved.th_approved_isApproved狀態,最後一個條目是最近的條目並且是正確的狀態

以下查詢的主要目的是選擇準備好被「定案」的備註。以下查詢的問題在最後一個inner join過濾器'AND th_Approved.th_approved_isApproved = 1'這是選擇有效批准的備註,這意味着它們應該在th_Approvedth_Approved.th_approved_isApproved中有條目。

這適用於th_Approved中單條目的筆記,但th_Approved(如上所述)中有多個條目的筆記表示如果該特定筆記的最後一項爲假,則表示此問題。該查詢仍會提取它,因爲至少有一個條目th_Approved.th_approved_isApproved爲真,即使上次的正確狀態爲假時也是如此。我只需要查看最後一個條目即可確定筆記的正確狀態,並根據狀態選擇或不選。

查詢的最後部分(and th_Therapy_Note.th_note_id=16239)僅供我測試,因爲本筆記有多個條目,但最終不會有這個。

我該如何解決我的問題?我一直在尋找,沒有運氣幾種策略.....希望我是有道理的:)謝謝

SELECT Distinct Convert(varchar,th_Therapy_Note.th_note_id) as NOTEID, '054' as PROGCODE, Rtrim(ch.child_caseNumber) as CASEID, 
         Case th_TherapyType.shortname when 'ST' then 'SP' else rtrim(th_TherapyType.shortname) end as SERVTYPE, Convert(varchar,th_Therapy_Note.th_note_dateofservice,101) as DELSERVDATE, 
         Cast(((Select sum(th_TherapyServiceProvided.units) From th_TherapyServiceProvided where th_DirectServices.th_ds_id = th_TherapyServiceProvided.th_ds_id)/60) as varchar) as SERVHRS, 
         Cast(((Select sum(th_TherapyServiceProvided.units) From th_TherapyServiceProvided where th_DirectServices.th_ds_id = th_TherapyServiceProvided.th_ds_id)%60) as varchar) as SERVMIN, 
        '1' as METHOD, isnull(th_Users.trad_id, ' ') as SPROVNUM, th_Users.th_user_lname, '' as COVISIT 
FROM   th_Therapy_Note INNER JOIN 
        child_tbl AS ch ON th_Therapy_Note.child_id = ch.child_recordId INNER JOIN 
        th_DirectServices ON th_Therapy_Note.th_note_id = th_DirectServices.th_note_id INNER JOIN 
        LookUp_contactType ON th_Therapy_Note.contact_type_id = LookUp_contactType.recId INNER JOIN 
        th_Users ON th_Therapy_Note.service_coordinator = th_Users.th_user_email INNER JOIN 
        th_TherapyType ON th_Therapy_Note.therapy_type = th_TherapyType.id INNER JOIN 
        th_Approved ON th_Therapy_Note.th_note_id = th_Approved.th_note_id AND th_Approved.th_approved_isApproved = 1 
WHERE  (ch.child_recordId = 
         (SELECT  MAX(child_recordId) AS Expr1 
         FROM   child_tbl 
         WHERE  (child_caseNumber = ch.child_caseNumber))) 
     and th_Therapy_Note.th_note_dateofservice > '4/22/2014' and th_Therapy_Note.th_note_id=16239 
+0

你不應該把那isApproved條件的JOIN ON子句中,但在哪裏。在JOIN中,您必須找到識別最後一條記錄的方法。什麼區別th_Approved行?一個約會?順序ID? – Frazz

+0

順序標識符'th_approved_id'並且它們還有日期時間標籤'th_approved_timestamp' – esausilva

+0

順序標識符是完美的。 – Frazz

回答

0

由於您在th_Approved上有順序標識,那麼我會使用它。整數比較id是完美的。日期/日期時間比較有時會增加問題。

所以我想試試這個:

SELECT Distinct 
    Convert(varchar,th_Therapy_Note.th_note_id) as NOTEID, 
    '054' as PROGCODE, 
    Rtrim(ch.child_caseNumber) as CASEID, 
    Case th_TherapyType.shortname 
    when 'ST' then 'SP' 
    else   rtrim(th_TherapyType.shortname) 
    end as SERVTYPE, 
    Convert(varchar,th_Therapy_Note.th_note_dateofservice,101) as DELSERVDATE, 
    Cast(((
    Select sum(th_TherapyServiceProvided.units) 
    From th_TherapyServiceProvided 
    where th_DirectServices.th_ds_id = th_TherapyServiceProvided.th_ds_id)/60) as varchar 
) as SERVHRS, 
    Cast(((
    Select sum(th_TherapyServiceProvided.units) 
    From th_TherapyServiceProvided 
    where th_DirectServices.th_ds_id = th_TherapyServiceProvided.th_ds_id)%60) as varchar 
) as SERVMIN, 
    '1' as METHOD, 
    isnull(th_Users.trad_id, ' ') as SPROVNUM, 
    th_Users.th_user_lname, '' as COVISIT 
FROM th_Therapy_Note 
INNER JOIN child_tbl AS ch ON th_Therapy_Note.child_id = ch.child_recordId 
INNER JOIN th_DirectServices ON th_Therapy_Note.th_note_id = th_DirectServices.th_note_id INNER JOIN LookUp_contactType ON th_Therapy_Note.contact_type_id = LookUp_contactType.recId INNER JOIN th_Users ON th_Therapy_Note.service_coordinator = th_Users.th_user_email 
INNER JOIN th_TherapyType ON th_Therapy_Note.therapy_type = th_TherapyType.id 
INNER JOIN th_Approved ON th_Approved.th_approved_id=(
    SELECT MAX(th_approved_id) 
    FROM th_Approved 
    WHERE th_Therapy_Note.th_note_id = th_Approved.th_note_id) 
WHERE ch.child_recordId = (
    SELECT MAX(child_recordId) 
    FROM child_tbl 
    WHERE child_caseNumber = ch.child_caseNumber) 
    AND th_Therapy_Note.th_note_dateofservice > '4/22/2014' 
    AND th_Approved.th_approved_isApproved = 1 
    AND th_Therapy_Note.th_note_id=16239 
+0

這工作完美,謝謝:) – esausilva

0

無法訪問您的數據集與此,以測試這是我可以給你最好的。 實質上,我正在使用CTE和TSQL命令ROW NUMBER使用您擁有的任何日期函數篩選出重複項。然後將過濾掉的列表放入主查詢中。

;with fixDuplicateCTE(
SELECT m.th_note_id, m.tag 
FROM (SELECT TH_NOTE_ID ROW_NUMBER OVER(partition by th_note_id ORDER BY [SOME DATE FUNCTION YOU HAVE!!!!!] desc) as tag FROM th_approved) as m 
INNER JOIN th_approved AS a on m.th_note_id = a.th_note_id 
WHERE m.tag = 1 
) 

SELECT Distinct Convert(varchar,th_Therapy_Note.th_note_id) as NOTEID, '054' as PROGCODE, Rtrim(ch.child_caseNumber) as CASEID, 
         Case th_TherapyType.shortname 
           when 'ST' then 'SP' else rtrim(th_TherapyType.shortname) end as SERVTYPE, Convert(varchar,th_Therapy_Note.th_note_dateofservice,101) as DELSERVDATE, 
         Cast(((Select sum(th_TherapyServiceProvided.units) From th_TherapyServiceProvided where th_DirectServices.th_ds_id = th_TherapyServiceProvided.th_ds_id)/60) as varchar) as SERVHRS, 
         Cast(((Select sum(th_TherapyServiceProvided.units) From th_TherapyServiceProvided where th_DirectServices.th_ds_id = th_TherapyServiceProvided.th_ds_id)%60) as varchar) as SERVMIN, 
        '1' as METHOD, isnull(th_Users.trad_id, ' ') as SPROVNUM, th_Users.th_user_lname, '' as COVISIT 
FROM   th_Therapy_Note 
INNER JOIN    child_tbl AS ch ON th_Therapy_Note.child_id = ch.child_recordId 
INNER JOIN     th_DirectServices ON th_Therapy_Note.th_note_id = th_DirectServices.th_note_id 
INNER JOIN     LookUp_contactType ON th_Therapy_Note.contact_type_id = LookUp_contactType.recId 
INNER JOIN     th_Users ON th_Therapy_Note.service_coordinator = th_Users.th_user_email 
INNER JOIN     th_TherapyType ON th_Therapy_Note.therapy_type = th_TherapyType.id 
INNER JOIN     fixDuplicateCTE ON th_Therapy_Note.th_note_id = th_Approved.th_note_id AND th_Approved.th_approved_isApproved = 1 
WHERE  (ch.child_recordId = 
         (SELECT  MAX(child_recordId) AS Expr1 
         FROM   child_tbl 
         WHERE  (child_caseNumber = ch.child_caseNumber))) 
     and th_Therapy_Note.th_note_dateofservice > '4/22/2014' and th_Therapy_Note.th_note_id=16239 
1

您可以使用「MAX」技巧(或「MIN」或類似技巧)。在一個日期或獨特的列是典型的。

這是一個通用的Northwind示例,它使用MAX(OrderDate)(客戶有多個訂單)。

如果有兩個訂單具有相同的訂單日期,那麼下面的邏輯就分解了,這些日期是「最大」日期。因此,可訂購的唯一標識符是首選)

Use Northwind 
GO 

Select cust.* , ords.* 
from dbo.Customers cust 

     LEFT OUTER JOIN dbo.Orders ords 

       ON 
       (
        ords.CustomerID = cust.CustomerID 

        AND ords.OrderDate = 
         (SELECT MAX(OrderDate) 
         FROM dbo.Orders innerords 
         WHERE innerords.CustomerID = cust.CustomerID 
         ) 
        ) 

where cust.CustomerID = 'ALFKI'