2016-01-05 37 views
0

我有一張表,我需要從一個組中獲取最早的日期並能夠返回所有行。我很難找到它,因爲我需要返回system_id字段。如何查找組中最早的日期

AssignedProfsHistory  MatterID  EffectiveDate 
     1     33434-3344  08/22/2005 
     2     33434-3344  07/12/2004 
     3     33434-3344  07/12/2004 
     4     21122-323  12/05/2007 
     5     43332-986  10/18/2014 
     6     43332-986  03/23/2013 

所以在這個例子中,SYSTEMID 2 & 3行應返回,因爲它們都被用於最早日期。 systemid 4的行應返回並返回systemid 6。

這是我到目前爲止。因爲我需要包含systemid(AssignedProfHistory),所以我沒有得到我需要的結果。

SELECT aph.AssignedProfsHistory, 
     m.MatterID, 
     Min(aph.EffectiveDate) as 'EffectiveDate' 
from AssignedProfsHistory aph 
INNER JOIN Matters m 
ON aph.Matters = m.Matters 
WHERE aph.AssignedType = 'Originating' 
Group by m.matters,m.matterid,aph.assignedprofshistory 
order by m.MatterID 

任何想法如何獲得我需要的結果?

預先感謝您。

回答

1

既然你想保持聯繫,我會做這樣的內容:

SELECT t2.AssignedProfsHistory, m.MatterID, t2.EffectiveDate 
FROM (
    SELECT MatterID, MIN(EffectiveDate) med 
    FROM AssignedProfsHistory 
    WHERE AssignedType = 'Originating' 
    GROUP BY MatterID 
) t1 
INNER JOIN AssignedProfsHistory t2 ON t2.MatterID = t1.MatterID 
    and t2.EffectiveDate = t1.med and t2.AssignedType = 'Originating' 
INNER JOIN Matters m on m.Matters = t2.Matters 
ORDER BY m.MatterId 

這裏是沒有Matters表的SQLFiddle那證明它可以工作,不需要窗口函數或CTE,儘管CTE可以避免重複AssignedType='Originating'條件。

+0

這個腳本適用於我。謝謝! – user3119773

2
select AssignedProfsHistory, MatterID, EffectiveDate 
from (
SELECT 
aph.AssignedProfsHistory, 
m.MatterID, 
aph.EffectiveDate, 
row_number() over(partition by m.MatterID order by aph.EffectiveDate) as rn 
from AssignedProfsHistory aph 
INNER JOIN Matters m ON aph.Matters = m.Matters 
WHERE aph.AssignedType = 'Originating' 
) t 
where rn = 1; 

您可以使用窗口函數row_number爲每個事件標識的日期分配行號。由於排序基於升序EffectiveDate,所以將具有最早日期的行分配爲1,然後選擇這些行。

如果一個事件可以包含多個具有最早日期的行,則可以使用rankdense_rank獲取最早日期的所有行。

+2

RANK或DENSE_RANK而不是ROW_NUMBER。他們想保持聯繫。 –

1

這應該得到你需要

with cte as (
        SELECT aph.AssignedProfsHistory, 
          m.MatterID, 
          aph.EffectiveDate as 'EffectiveDate' 
        from AssignedProfsHistory aph 
        INNER JOIN Matters m 
        ON aph.Matters = m.Matters 
        WHERE aph.AssignedType = 'Originating' 
        Group by m.matters,m.matterid,aph.assignedprofshistory 
       ) 



    select 
     AssignedProfsHistory, 
     MatterID, 
     EffectiveDate 
    from 
     cte 
     join (
       select 
        min(EffectiveDate) min_effectivedate, 
        MatterID 
       from 
        cte 
       group by 
        MatterID 
       ) b on cte.EffectiveDate = b.min_effectivedate and 

cte.MatterID = b.MatterID 


order by AssignedProfsHistory 
1

先把舊的日期,然後加入你的表。

WITH OlderAPH AS (
    SELECT 
     AssignedProfsHistory, 
     Matters, 
     MIN(EffectiveDate) OlderDate 
    FROM AssignedProfsHistory 
    WHERE AssignedType = 'Originating' 
    GROUP BY Matters, AssignedProfsHistory) 
SELECT 
    O.AssignedProfsHistory, M.MatterID, O.OlderDate 
FROM OlderAPH O 
    INNER JOIN Matters M ON O.Matters = M.Matters  
ORDER BY M.MatterID 
相關問題