获得最大行的两种不同方法

时间:2016-10-19 17:46:22

标签: sql sql-server min window-functions

第一个陈述是我需要根据我工作的组织需求拉出最小行。首先,我会MIN(DATEFIELD),但如果有人在同一天有两个条目,我们就会遇到问题。接下来我尝试了MIN(OP__DOCID),其中OP__DOCID是表的唯一键。这里的问题是,如果某人忘记了他们忘记创建的条目,结果将是不准确的。所以,我想出了以下声明。它确保我从每个独特的录取中获得最新结果。

SELECT OP__DocID
FROM FD__CNSLG_BASIS24 AS PC1
WHERE (OP__DOCID = 
        (SELECT TOP(1)OP__DocID
            FROM FD__CNSLG_BASIS24 AS PC2
            WHERE PC2.ClientKey = PC1.Clientkey and PC2.ProgramAdmitKey = PC1.Programadmitkey
            ORDER BY Date_Screening
        )
    )

最近,我已经了解了OVER(PARTITION BY)并且对于它如何工作的微妙差异感到好奇。上面的陈述,因为我得到了不同的结果。

SELECT OP__DocID = Min(OP__DOCID) OVER (Partition BY Clientkey, Programadmitkey)
FROM FD__CNSLG_BASIS24

任何见解或指向我能阅读的其他页面的链接都会非常有帮助。

谢谢!

2 个答案:

答案 0 :(得分:1)

只需使用窗口功能:

select pc.*
from (select pc.*,
             row_number() over (partition by Clientkey, ProgramAdmitKey
                                order by Date_Screening  -- do you mean DESC?
                               ) as seqnum
      from FD__CNSLG_BASIS24 PC
     ) pc
where seqnum = 1;

注意:这将根据筛选日期获得第一条记录。您可能希望DESC获取最新信息。

答案 1 :(得分:0)

我的解决方案,对于那些好奇的人

我想回去用SELECT TOP(1)代替ROW_Number()函数,但我需要得到一个报告,这就是我需要的东西。感谢大家的帮助。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
BEGIN
    SET NOCOUNT ON;

Declare 
@StartDate Date,
@EndDate Date

SET @StartDate = '1/1/2016'
SET @EndDate = '6/1/2016'

WITH CNSL_Clients AS (
                SELECT PC_CNT.Clientkey, PC_Cnt.ProgramAdmitKey, PC_Cnt.OP__DOCID
                FROM FD__Primary_Client as PC_Cnt
                                INNER JOIN VW__Cnsl_Session_Count_IndvFamOnly as cnt
                                    ON PC_Cnt.Clientkey = CNT.Clientkey AND PC_Cnt.ProgramAdmitKey = CNT.ProgramAdmitKey
                WHERE ((pc_CNT.StartDate between @StartDate AND @EndDate) OR (pc_CNT.StartDate <= @StartDate AND pc_CNT.ENDDate >= @StartDate) OR (pc_CNT.StartDate <= @StartDate AND pc_CNT.ENDDate is null)) 
                AND CNT.SessionCount>=6
),

FIRST_BASIS AS (
            SELECT CB24_1.OP__DOCID, CB24_1.Date_Screening, CB24_1.ClientKey, CB24_1.ProgramAdmitKey, CB24_1.Composite_score, CB24_1.Depression_Results,CB24_1.Emotional_Results, CB24_1.Relationships_Results
            FROM FD__CNSLG_BASIS24 AS CB24_1
            WHERE (CB24_1.OP__DOCID =
                    (Select TOP(1) CB24_2.OP__DOCID
                    FROM FD__CNSLG_BASIS24 AS CB24_2
                                        Inner JOIN CNSL_Clients
                                            ON CB24_2.ClientKey = CNSL_Clients.ClientKey AND CB24_2.ProgramAdmitKey = CNSL_Clients.ProgramAdmitKey
                    WHERE (CB24_1.ClientKey = CB24_2.ClientKey) AND (CB24_1.ProgramAdmitKey = CB24_2.ProgramAdmitKey)
                    ORDER BY CB24_2.Date_Screening))
),

RECENT_BASIS AS (
            SELECT CB24_1.OP__DOCID, CB24_1.Date_Screening, CB24_1.ClientKey, CB24_1.ProgramAdmitKey, CB24_1.Composite_score, CB24_1.Depression_Results,CB24_1.Emotional_Results, CB24_1.Relationships_Results
            FROM FD__CNSLG_BASIS24 AS CB24_1
            WHERE (CB24_1.OP__DOCID =
                    (Select TOP(1) CB24_2.OP__DOCID
                    FROM FD__CNSLG_BASIS24 AS CB24_2
                                        Inner JOIN CNSL_Clients
                                            ON CB24_2.ClientKey = CNSL_Clients.ClientKey AND CB24_2.ProgramAdmitKey = CNSL_Clients.ProgramAdmitKey
                    WHERE (CB24_1.ClientKey = CB24_2.ClientKey) AND (CB24_1.ProgramAdmitKey = CB24_2.ProgramAdmitKey)
                    ORDER BY CB24_2.Date_Screening DESC))
)

SELECT F.OP__DOCID AS First_DOCID,R.OP__DOCID as Recent_DOCID,F.ClientKey, F.ProgramAdmitKey, F.Composite_Score AS FComposite_Score, R.Composite_Score as RComposite_Score, Composite_Change = R.Composite_Score - F.Composite_Score, F.Depression_Results AS FDepression_Results, R.Depression_Results AS RDepression_Resluts, Depression_Change = R.Depression_Results - F.Depression_Results, F.Emotional_Results AS FEmotional_Resluts, R.Emotional_Results AS REmotionall_Reslu, Emotional_Change = R.Emotional_Results - F.Emotional_Results, F.Relationships_Results AS FRelationships_Resluts, R.Relationships_Results AS RRelationships_Resluts, Relationship_Change = R.Relationships_Results - F.Relationships_Results
FROM First_basis AS F
        FULL Outer JOIN RECENT_BASIS AS R
            ON F.ClientKey = R.ClientKey AND F.ProgramAdmitKey = R.ProgramAdmitKey
ORDER BY F.ClientKey
END
GO