TSQL用于获取DISTINCT记录,然后应用行号

时间:2011-02-24 22:02:29

标签: sql-server tsql sql-server-2008

我问了一个关于在软件激活方案中处理锁定的问题,其中较旧的激活在处理较新的激活时被锁定。

SQL Server Stored Procedure to dump oldest X records when new records added

我遇到了一个问题,希望有人可以提供帮助。我们假设Activations表包含以下列:

CustomerName,ProductName,KeyCode,ActivationDate

我需要获取唯一的CustomerName,ProductName和KeyCode数据,因为您可以多次激活同一台机器。结果集需要由ActivationDate DESC排序,以便我可以按激活的顺序处理数据。因此,在我的场景中,我可以允许最后两次激活工作,所有这些都在登录到Lockouts表之前,因此它们被锁定,并且记录了新的激活。

如何订购唯一/不同的结果集然后应用行号以便我可以迭代结果集并丢弃最新的激活并使用较旧的激活来锁定它们?

谢谢。

2 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西: 您希望行号的所有行都不是最大ActivationDate

DECLARE @tbl TABLE
(
    CustomerName    VARCHAR(20),
    ProductName     VARCHAR(20),
    KeyCode         INT,
    ActivationDate  DATETIME
)


INSERT INTO @tbl
SELECT 'cmp1', 'game', 28734, GETDATE() -1    UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -1.5  UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -1.2  UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -1.8  UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE()       UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -17   UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1    UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1.5  UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1.2  UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1.8  UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE()       UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -17     


SELECT        ROW_NUMBER() OVER(ORDER BY ActivationDate DESC) RowNumber,
              CustomerName, 
              ProductName, 
              KeyCode, 
              ActivationDate        
FROM @tbl workTable
WHERE ActivationDate != 
    (
        SELECT MAX(ActivationDate)
        FROM @tbl checkTable
        WHERE workTable.CustomerName = checkTable.CustomerName
            AND workTable.ProductName = checkTable.ProductName
            AND workTable.KeyCode = checkTable.KeyCode
    )

RowNumber     CustomerName     ProductName     KeyCode  ActivationDate
1             cmp1             game            28734    2011-02-24 08:40:45.790
2             cmp2             game            28736    2011-02-24 08:40:45.790
3             cmp2             game            28736    2011-02-24 03:52:45.793
4             cmp1             game            28734    2011-02-24 03:52:45.793
5             cmp1             game            28734    2011-02-23 20:40:45.790
6             cmp2             game            28736    2011-02-23 20:40:45.790
7             cmp2             game            28736    2011-02-23 13:28:45.790
8             cmp1             game            28734    2011-02-23 13:28:45.790
9             cmp1             game            28734    2011-02-08 08:40:45.790
10            cmp2             game            28736    2011-02-08 08:40:45.790

如果你想要不止一个......

SELECT  ROW_NUMBER() OVER(ORDER BY ActivationDate DESC) RowNumber,
        CustomerName, 
        ProductName, 
        KeyCode, 
        ActivationDate      
FROM @tbl workTable
WHERE NOT ActivationDate IN 
    (
        SELECT TOP 2 ActivationDate
        FROM @tbl checkTable
        WHERE workTable.CustomerName = checkTable.CustomerName
            AND workTable.ProductName = checkTable.ProductName
            AND workTable.KeyCode = checkTable.KeyCode
        ORDER BY ActivationDate DESC
    )

答案 1 :(得分:0)

我认为这会做你想要的:

select t.CustomerName, t.ProductName, t.KeyCode, t.MaxDate,
       row_number() over (order by t.MaxDate desc) as RowNum
    from (select CustomerName, ProductName, KeyCode, max(ActivationDate) as MaxDate
              from Activations
              group by CustomerName, ProductName, KeyCode) t

也可以使用CTE编写,如下所示:

;with cteMaxDate as (
    select CustomerName, ProductName, KeyCode, max(ActivationDate) as MaxDate
        from Activations
        group by CustomerName, ProductName, KeyCode
)
select t.CustomerName, t.ProductName, t.KeyCode, t.MaxDate,
       row_number() over (order by t.MaxDate desc) as RowNum
    from cteMaxDate t