如何在Group中找到最早的日期

时间:2016-01-05 22:56:19

标签: sql sql-server

我有一张表,我需要从组中获取最早的日期,并能够返回所有行。我发现很难,因为我需要返回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

知道如何获得我需要的结果吗?

提前谢谢。

4 个答案:

答案 0 :(得分: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获取最早日期的所有行。

答案 1 :(得分: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

答案 2 :(得分: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'条件。 / p>

答案 3 :(得分: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