如何选择特定细节的最新记录

时间:2018-05-21 07:11:36

标签: sql sql-server

我在SQL服务器表中有数据,如下所示,

我有以下数据,

id  Cust_id   Name  Qty    Trans_date
_______________________________________

1   101     Srini   10    01-May-2018
2   101     Srini   20    01-May-2018
3   102     Arun    100   01-May-2018
4   102     Arun    200   01-May-2018
5   101     Srini   10    02-May-2018
6   101     Srini   30    02-May-2018

我想要下面的结果集,

5   101     Srini   10    02-May-2018
6   101     Srini   30    02-May-2018
3   102     Arun    100   01-May-2018
4   102     Arun    200   01-May-2018

我需要查询以获取最新的转换日期详细信息。 Srini的最新纪录是5月2日,Arun的最新纪录是5月1日。

7 个答案:

答案 0 :(得分:1)

这个问题可以通过行号方法的不同来很好地处理。此方法相当强大,并且可以容忍单独的客户记录孤岛具有相同日期的情况。要了解其工作原理,请参阅演示;用文字解释并不容易。

cte1 AS (
    SELECT *,
        ROW_NUMBER() OVER (ORDER BY id) -
            ROW_NUMBER() OVER (PARTITION BY Cust_id ORDER BY id) diff
    FROM yourTable
),
cte2 AS (
    SELECT *,
        DENSE_RANK() OVER (PARTITION BY Cust_id ORDER BY diff DESC) rank
    FROM cte1
)

SELECT id, Cust_id, Name, Qty, Trans_date
FROM cte2
WHERE rank = 1;

Demo

答案 1 :(得分:0)

DENSE_RANK()

select * 
from ( select * 
            , DENSE_RANK() over (partition by custid order by Trans_date desc) rn
         from table
     ) t
where t.rn = 1

答案 2 :(得分:0)

DENSE_RANK()功能应该是工作人员

Select * from ( select * 
        , DENSE_RANK() over (partition by cust_id order by Trans_date DESC) row_n from table_name
 ) A
Where A.row_n = 1

答案 3 :(得分:0)

使用此

SELECT * 
FROM   TABLE 
WHERE  TRANS_DATE = (SELECT Max(TRANS_DATE) 
                     FROM   TABLE) 
       AND NAME = "YOURCHOICE" 

答案 4 :(得分:0)

试试这个

;WITH CTE(id,  Cust_id ,  Name  ,Qty,    Trans_date)
AS
(
SELECT 1,101,'Srini', 10 ,'01-May-2018' UNION ALL
SELECT 2,101,'Srini', 20 ,'01-May-2018' UNION ALL
SELECT 3,102,'Arun' , 100,'01-May-2018' UNION ALL
SELECT 4,102,'Arun' , 200,'01-May-2018' UNION ALL
SELECT 5,101,'Srini', 10 ,'02-May-2018' UNION ALL
SELECT 6,101,'Srini', 30 ,'02-May-2018' 
)
SELECT ID, 
       CUST_ID, 
       NAME, 
       QTY, 
       TRANS_DATE 
FROM   (SELECT *, 
               ROW_NUMBER() 
                 OVER( 
                   PARTITION BY CUST_ID 
                   ORDER BY TRANS_DATE DESC) AS Rnk 
        FROM   CTE)Dt 
WHERE  Dt.RNK <= 2 

结果

ID  CUST_ID NAME    QTY  TRANS_DATE
------------------------------------
6   101 Srini       30   02-May-2018
5   101 Srini       10   02-May-2018
4   102 Arun        200  01-May-2018
3   102 Arun        100  01-May-2018

答案 5 :(得分:0)

不使用排名功能的替代解决方案。

这为其他解决方案提供了不同的执行计划,并且可能在您的环境中表现更好。

CREATE TABLE #TMP(id INT, 
                 Cust_id SMALLINT,  
                 Name  VARCHAR(50) ,
                 Qty SMALLINT,    
                 Trans_date DATE)

INSERT INTO #TMP VALUES 
( 1,101,'Srini', 10 ,'01-May-2018' ),
( 2,101,'Srini', 20 ,'01-May-2018' ),
( 3,102,'Arun' , 100,'01-May-2018' ),
( 4,102,'Arun' , 200,'01-May-2018' ),
( 5,101,'Srini', 10 ,'02-May-2018' ),
( 6,101,'Srini', 30 ,'02-May-2018' )


;WITH cte AS (
SELECT
    Cust_id,MAX(Trans_Date) MaxTrans
FROM    
    #TMP
GROUP BY Cust_id
)
SELECT
    T.*
FROM
    #TMP T
        INNER JOIN cte C
            ON
            T.Cust_id = C.Cust_id
            AND
            T.Trans_date = C.MaxTrans

答案 6 :(得分:0)

您可以使用subquery

select t.*
from table t
where Trans_date = (select max(Trans_date) 
                    from table t1
                    where t1.cust_id = t.cust_id
                   );