2

我正在尝试搜索最近发生的一堆交易,但只希望每笔交易返回一次。我对代码问题的尝试比我能解释得更好。

SELECT 
DISTINCT    TransactionCode
    ,       IdKey 
FROM        TransTable 
WHERE       TransactionCode IN (<massive list of ids...>) 
AND         ActionDate      < GETDATE() 
ORDER BY    ActionDate DESC

我想要每个事务代码的一个实例,按日期排序(也就是该事务的最近发生),并且还返回了 idKey。想法?

4

3 回答 3

5
SELECT TransactionCode, 
       IdKey
FROM
  (
    SELECT TransactionCode, 
           IdKey,
           ROW_NUMBER() OVER(PARTITION BY TransactionCode 
                             ORDER BY ActionDate DESC) AS rn
    FROM TransTable 
    WHERE TransactionCode in (1,2,3) AND 
          ActionDate < GETDATE() 
  ) T
WHERE rn = 1
于 2012-04-30T11:29:57.057 回答
2

您需要通过在TransactionCode列上对它们进行分区来为表中的每一行分配行号,然后ActionDate按降序对它们进行排序,以便您在该部分的顶部获得最新的事务。根据此逻辑分配行号后,您可以仅从派生表输出中过滤掉rownum值为1的行。这将获取所有交易代码。您可以根据需要在以下查询中添加过滤条件。

单击此处查看 SQL Fiddle 中的演示

脚本

CREATE TABLE dbo.TransTable
(
        IdKey           INT         NOT NULL IDENTITY
    ,   TransactionCode VARCHAR(10) NOT NULL
    ,   ActionDate      DATETIME    NOT NULL
);

INSERT INTO dbo.TransTable (TransactionCode, ActionDate) VALUES
    ('code 1', '2012-04-27 01:04:12.467'),
    ('code 1', '2012-04-22 09:16:29.354'),
    ('code 2', '2012-04-12 11:04:27.751'),
    ('code 1', '2012-06-19 12:27:12.232'),
    ('code 2', '2012-04-04 05:22:17.467'),
    ('code 3', '2012-05-01 08:49:12.951'),
    ('code 3', '2012-05-13 06:12:12.234');

SELECT  IdKey
    ,   TransactionCode
    ,   ActionDate
FROM
(
    SELECT  IdKey
        ,   TransactionCode
        ,   ActionDate
        ,   ROW_NUMBER() OVER (
                        PARTITION  BY TransactionCode 
                        ORDER BY ActionDate DESC
            ) rownum
    FROM    dbo.TransTable
    WHERE   ActionDate < GETDATE()
) t1 WHERE rownum = 1;

输出

IdKey TransactionCode ActionDate
----- --------------- -----------------------
1     code 1          2012-04-27 01:04:12.467
3     code 2          2012-04-12 11:04:27.750
于 2012-04-30T11:33:07.320 回答
1

可能是这样,你可以找到它:

;WITH Cte AS (
SELECT 
    ROW_Number() over (partition by TransactionCode, IdKey  ORDER BY ActionDate DESC) RowID,
    TransactionCode, IdKey      
FROM TransTable 
WHERE TransactionCode in (Massive list of IDs) 
AND ActionDate < GETDATE() 
)
SELECT * FROM Cte WHERE RowID = 1
于 2012-04-30T11:28:39.660 回答