1

所以我使用的数据库没有一个很好的方法来通过它的唯一 ID 选择最近的数字。我们必须缩小范围以获取最近的记录,其中包含一堆连接到原始表的子查询。原始表是 TBL_POL。前任。

Policy_ID   Load_DATE   ENDORSEMENT#    SEQUENCE    EXTRACTDATE
25276       8/16/2015       0               1        8/15/2015      
25276       2/13/2016       1               2        2/12/2016
25276       9/24/2016       3               4        9/20/2016
25276       9/24/2016       3               4        9/20/2016
25276       9/24/2016       2               3        9/20/2016

所以首先我们获取最大加载日期并加入原始表,然后获取最大背书#,然后重新加入并获取最大序列,然后重新加入并获取最大提取日期,最终回到我们的最终记录,所以这将是独一无二的。上面是一个例子。

有没有更简单的方法来做到这一点?有人提到 row_number() over(partition by),但我认为这只会返回您想要的任何行号。我想要一种快速的方法,一次滑动即可获取所有上述所有属性的最多记录。有没有人有更好的主意来做到这一点,因为这些查询需要一些时间来运行。

谢谢

4

2 回答 2

2

@布莱恩特,

首先,@Backs 为您保存了这篇文章。当我第一次看到它时,我想“该死。如果他不想花时间让他的请求可读,我为什么要打扰”?此外,如果您正在寻找一个编码示例,那么最好创建一些易于使用的测试数据,以使人们更容易帮助您。此外,正如@Felix Pamittan 建议的那样,您还应该发布您的预期回报。

这是发布易于使用的测试数据的一种方法。我还添加了另一个 Policy_ID,以便我可以演示如何对整个表执行此操作,而不仅仅是一个 Policy_ID。

--===== If the test table doesn't already exist, drop it to make reruns in SSMS easier.
     -- This is NOT a part of the solution. We're just simulating the original table
     -- using a Temp Table.
     IF OBJECT_ID('tempdb..#TBL_POL','U') IS NOT NULL
   DROP TABLE #TBL_POL
;
--===== Create the test table (technically, a heap because no clustered index)
     -- Total SWAG on the data-types because you didn't provide those, either.
 CREATE TABLE #TBL_POL
        (
         Policy_ID      INT     NOT NULL
        ,Load_DATE      DATE    NOT NULL
        ,ENDORSEMENT#   TINYINT NOT NULL
        ,SEQUENCE       TINYINT NOT NULL
        ,EXTRACTDATE    DATE    NOT NULL
        )
;
--===== Populate the test table
 INSERT INTO #TBL_POL
        (Policy_ID,Load_DATE,ENDORSEMENT#,SEQUENCE,EXTRACTDATE)
 SELECT Policy_ID,Load_DATE,ENDORSEMENT#,SEQUENCE,EXTRACTDATE
   FROM (VALUES
        --===== Original values provided
         (25276,'8/16/2015',0,1,'8/15/2015')  
        ,(25276,'2/13/2016',1,2,'2/12/2016')
        ,(25276,'9/24/2016',3,4,'9/20/2016')
        ,(25276,'9/24/2016',3,4,'9/20/2016')
        ,(25276,'9/24/2016',2,3,'9/20/2016')
        --===== Additional values to demo multiple Policy_IDs with
        ,(12345,'8/16/2015',0,1,'8/15/2015')  
        ,(12345,'9/24/2016',1,5,'2/12/2016')
        ,(12345,'2/13/2016',1,2,'2/12/2016')
        ,(12345,'9/24/2016',3,4,'9/20/2016')
        ,(12345,'9/24/2016',3,4,'9/20/2016')
        ,(12345,'9/24/2016',2,3,'9/20/2016')

        ) v (Policy_ID,Load_DATE,ENDORSEMENT#,SEQUENCE,EXTRACTDATE)
;
--===== Show what's in the test table
 SELECT * 
   FROM #TBL_POL
;

如果您希望一次解决多个 Policy_ID 的问题,则以下方法将起作用。

--===== Use a partitioned windowing function to find the latest row
     -- for each Policy_ID, ignoring "dupes" in the process.
     -- This assumes that the "sequence" column is king of the hill.
   WITH cteEnumerate AS
        (
         SELECT *
                ,RN = ROW_NUMBER() OVER (PARTITION BY Policy_ID ORDER BY SEQUENCE DESC)
           FROM #TBL_POL
        )
 SELECT Policy_ID,Load_DATE,ENDORSEMENT#,SEQUENCE,EXTRACTDATE
   FROM cteEnumerate
  WHERE RN = 1
;

如果您只是为此寻找一个 Policy_ID,@ZLK 建议的“TOP 1”方法将起作用,但在上面添加 WHERE 子句也是如此。不确定哪个会更快,但相同的索引会对两者都有帮助。这是带有 WHERE 子句(可以参数化)的解决方案。

--===== Use a partitioned windowing function to find the latest row
     -- for each Policy_ID, ignoring "dupes" in the process.
     -- This assumes that the "sequence" column is king of the hill.
   WITH cteEnumerate AS
        (
         SELECT *
                ,RN = ROW_NUMBER() OVER (PARTITION BY Policy_ID ORDER BY SEQUENCE DESC)
           FROM #TBL_POL
          WHERE Policy_ID = 25276
        )
 SELECT Policy_ID,Load_DATE,ENDORSEMENT#,SEQUENCE,EXTRACTDATE
   FROM cteEnumerate
  WHERE RN = 1
;
于 2017-05-05T02:12:22.347 回答
0

也许你应该尝试 Grouping SET

抛出另一个样本数据。我也不确定性能。

提供反馈,但结果和表现都

SELECT *
FROM (
    SELECT Policy_ID
        ,max(Load_DATE) Load_DATE
        ,max(ENDORSEMENT#) ENDORSEMENT#
        ,max(SEQUENCE) SEQUENCE
        ,max(EXTRACTDATE) EXTRACTDATE
    FROM #TBL_POL t
    GROUP BY grouping SETS(Policy_ID, Load_DATE, ENDORSEMENT#, SEQUENCE, EXTRACTDATE)
    ) t4
WHERE Policy_ID IS NOT NULL

     drop table #TBL_POL
于 2017-05-05T04:56:04.107 回答