简体   繁体   中英

Distinct query for SQL Server needed

I am pulling data with this query in SQL Server

SELECT DISTINCT
        DOC.TPID,
        DOC.TYPE,
        DOC.DOCNO,
        O211.PONO,
        H210.INVDATE,
        H210.INVNO,
        H210.EQPMTINIT,
        H210.EQPMTNO,
        D214.DESTIMATED,
        D214.DACTUAL,
        DOC.CDATETIME
FROM [databasename].[dbo].[DOC]
JOIN [databasename].[dbo].[IN_211_HDR] H211 ON DOC.[TRANNO] = H211.TRANNO
JOIN [databasename].[dbo].[IN_211_ORD] O211 ON H211.TRANNO = O211.TRANNO
JOIN [databasename].[dbo].[IN_210_HDR] H210 ON DOCNO = H210.BOLNO
JOIN [databasename].[dbo].[IN_214_HDR] H214 ON H211.BOLNO = H214.SHPID
JOIN  [databasename].[dbo].[IN_214_DTL] D214 ON H214.TRANNO = D214.TRANNO
WHERE 
    [TPID]  = 'DSV' AND doc.[STATUSERP] = ''
ORDER BY 
    CDATETIME DESC

This will return the following result set.

                       O211.PONO                                                           D214.DESTIMATED
DSV  211  STAD8204126   106824  2014-05-27 00:00:00.000 US01271338  CCLU    4481776    2014-04-20 00:00:00.000  NULL    2014-04-10 15:00:10.000
DSV  211  STAD8204126   106824  2014-05-27 00:00:00.000 US01271338  CCLU    4481776    2014-05-02 00:00:00.000  NULL    2014-04-10 15:00:10.000
DSV  211  STAD8204126   106824  2014-05-27 00:00:00.000 US01271338  CCLU    4481776    2014-05-03 00:00:00.000  NULL    2014-04-10 15:00:10.000
DSV  211  STAD8204126   106824  2014-05-27 00:00:00.000 US01271338  CCLU    4481776    2014-05-18 00:00:00.000  NULL    2014-04-10 15:00:10.000
DSV  211  STAD8203444   106843  2014-05-21 00:00:00.000 US01267372  TGHU    4732265    2014-04-17 00:00:00.000  NULL    2014-04-10 08:03:14.000
DSV  211  STAD8203444   106843  2014-05-21 00:00:00.000 US01267372  TGHU    4732265    2014-05-05 00:00:00.000  NULL    2014-04-10 08:03:14.000
DSV  211  STAD8203444   106847  2014-05-21 00:00:00.000 US01267372  TGHU    4732265    2014-04-17 00:00:00.000  NULL    2014-04-10 08:03:14.000
DSV  211  STAD8203444   106847  2014-05-21 00:00:00.000 US01267372  TGHU    4732265    2014-05-05 00:00:00.000  NULL    2014-04-10 08:03:14.000
DSV  211  STAD8203444   108380  2014-05-21 00:00:00.000 US01267372  TGHU    4732265    2014-04-17 00:00:00.000  NULL    2014-04-10 08:03:14.000
DSV  211  STAD8203444   108380  2014-05-21 00:00:00.000 US01267372  TGHU    4732265    2014-05-05 00:00:00.000  NULL    2014-04-10 08:03:14.000

I need to have it so that it only returns rows with a unique O211.PONO. The only difference between those rows is the date but I need to only return one row for each unique O211.PONO number. It should take the one with the latest date in the D214.DESTIMATED field.

The easiest way is with row_number() :

with t as (
      <your query here without the order by>
     )
select t.*
from (select t.*,
             row_number() over (partition by PONO order by DESTIMATED desc) as seqnum
      from t
     ) t
where seqnum = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM