7

以下是我在 SQL Server 2008 数据库中的一个表的子集。

我正在尝试输出具有最大连续值计数的Serial Number,以及实际计数。需要注意的是,表格是按值降序排列的(这个条件很关键)。它可以按升序或降序进行分组或排序......任何更有效且更有意义的方式。 LIDLIDLast Updated DateSerial NumberSerial Number

数据如下所示:

[Serial Number]  [LID]   [Last Updated Date]
--------------------------------------    
123456            AAA     2012-09-24
123456            AAA     2012-09-23
123456            AAA     2012-09-22
123456            AAA     2012-09-21
123456            BBB     2012-09-20
123456            BBB     2012-09-19
123456            AAA     2012-09-18
123456            AAA     2012-09-17
123456            AAA     2012-09-16
234567            BBB     2012-09-24
234567            BBB     2012-09-23
234567            AAA     2012-09-22

表的期望输出是:

[Serial Number]     [LID]     [LID Count]
-------------------------------------------    
123456            AAA     4
234567            BBB     2

我很茫然。我试过使用

ROW_NUMBER() OVER(PARTITION BY [Service Tag], [LID]
                  ORDER BY [Last Updated Date] DESC) 

但所做的只是打破我的降序日期顺序,最后得到在日期范围内出现最多的计数和 LID。

提前感谢您提供的任何帮助!

此致,

副总裁

4

2 回答 2

4

看看下面的例子

SQL 小提琴演示

DECLARE @Table TABLE(
        [Serial Number] INT,
        [LID] VARCHAR(50),
        [Last Updated Date] DATETIME
)

INSERT INTO @Table SELECT 123456,'AAA','2012-09-24'
INSERT INTO @Table SELECT 123456,'AAA','2012-09-23'
INSERT INTO @Table SELECT 123456,'AAA','2012-09-22'
INSERT INTO @Table SELECT 123456,'AAA','2012-09-21'
INSERT INTO @Table SELECT 123456,'BBB','2012-09-20'
INSERT INTO @Table SELECT 123456,'BBB','2012-09-19'
INSERT INTO @Table SELECT 123456,'AAA','2012-09-18'
INSERT INTO @Table SELECT 123456,'AAA','2012-09-17'
INSERT INTO @Table SELECT 123456,'AAA','2012-09-16'
INSERT INTO @Table SELECT 234567,'BBB','2012-09-24'
INSERT INTO @Table SELECT 234567,'BBB','2012-09-23'
INSERT INTO @Table SELECT 234567,'AAA','2012-09-22'

;WITH Vals AS (
        SELECT  *,
                ROW_NUMBER() OVER(ORDER BY [Serial Number],[Last Updated Date] DESC) ROWID
        FROM    @Table
)
, ValsNext AS (
        SELECT  v.[Serial Number],
                v.LID,
                v.[Last Updated Date],
                v.ROWID,
                MIN(vn.ROWID) NextRowID
        FROM    Vals v LEFT JOIN
                Vals vN ON  v.[Serial Number] = vn.[Serial Number]
                        AND v.LID != vn.LID
                        AND v.ROWID < vn.ROWID
        GROUP BY    v.[Serial Number],
                    v.LID,
                    v.[Last Updated Date],
                    v.ROWID
)
, ValDiffs AS (
        SELECT  vn.[Serial Number],
                vn.LID,
                vn. NextRowID - vn.ROWID Consecutive
        FROM    ValsNext vn
)
, Serials AS (
        SELECT  [Serial Number],
                MAX(Consecutive) MaxConsecutive
        FROM    ValDiffs
        GROUP BY    [Serial Number]
)
SELECT  vd.*
FROM    Serials s INNER JOIN
        ValDiffs vd ON  s.[Serial Number] = vd.[Serial Number]
                    AND s.MaxConsecutive = vd.Consecutive
于 2012-09-27T04:29:13.803 回答
0

这是一个解决方案

;with cte as (
  select sn, lid, ludate,
    datediff(d,row_number() over (
    partition by sn,lid
    order by ludate),ludate) dd
  from xtable
), cte2 as (
  select sn,lid,dd,count(*)c,
      rn=row_number() over (partition by lid
                            order by count(*) desc)
  from cte
  group by sn,lid,dd
)
select sn [Serial Number], lid, c [Count]
from cte2
where rn=1

第一个 CTE 使用复杂的 DATEADD 表达式将连续的天组合在一起,使用 column 标记dd
第二个 CTE 是计数的常规行编号。

于 2012-09-27T04:48:23.627 回答