0

下面的查询为我提供了我需要的所有信息。但是,由于修订级别,我得到了多个结果。大多数结果的第一个也是唯一一个修订返回一个空白。如果报价已被修订,则分配给修订 A。如果再次修订,则得到 B。我得到空白、A、B 等的结果。我想将查询限制为仅查看最大值如果它不是空白的。

SELECT
 RTRIM(CUST_REF_NUMBER) AS "PICKUP #"
,RTRIM(SN.NUMBER) AS "NUMBER"
,DATE(PICKUP_TSTAMP) AS "PICKUP DATE"
,B.DUE_DATE
,WEIGHT
,TOTAL_PIECES AS "PIECES"
,V.QUOTE_ID AS "QUOTE"
,EQUIP AS "ACTUAL QUOTE PRICE"
,TOTAL_CHG AS "CHARGES"
,B.CUST AS "ACCOUNT NUMBER"
,C.ACCT_SPELLING AS "ACCOUNT NAME"
,H.REVISION

FROM AF.NOTIF SN
INNER JOIN AF.BILL B
ON SN.NUMBER = B.NUMBER
AND B.ARCHIVE_KEY = ''
AND B.CORRECTED = ''
AND B.TSTAMP >= SN.TSTAMP - 10 DAYS
INNER JOIN AF.PROS V
ON SN.NUMBER = V.NUMBER
INNER JOIN AF.CHARGES H
ON V.QUOTE_ID = H.QUOTE_ID
AND H.STATUS = 'X'
LEFT OUTER JOIN AF.SPELLING C
ON B.CUST = C.ACCT_NUM
AND C.TYPE = 'M'

WHERE WEB_ID IN ('XXXXXXX','XXXXXXX','XXXXXXX')
AND SN.TSTAMP > TIMESTAMP(CHAR(CURRENT DATE - 7 DAYS)||'-00.00.00.000000')
ORDER BY PICKUP_TSTAMP
4

2 回答 2

0

像这样的东西,可能是:

SELECT * FROM (
    SELECT
     RTRIM(CUST_REF_NUMBER) AS "PICKUP #"
    ,RTRIM(SN.NUMBER) AS "NUMBER"
    ,DATE(PICKUP_TSTAMP) AS "PICKUP DATE"
    ,B.DUE_DATE
    ,WEIGHT
    ,TOTAL_PIECES AS "PIECES"
    ,V.QUOTE_ID AS "QUOTE"
    ,EQUIP AS "ACTUAL QUOTE PRICE"
    ,TOTAL_CHG AS "CHARGES"
    ,B.CUST AS "ACCOUNT NUMBER"
    ,C.ACCT_SPELLING AS "ACCOUNT NAME"
    ,H.REVISION

    ,ROW_NUMBER() OVER (PARTITION BY V.QUOTE_ID ORDER BY H.REVISION DESC) RN

    FROM AF.NOTIF SN
    INNER JOIN AF.BILL B
    ON SN.NUMBER = B.NUMBER
    AND B.ARCHIVE_KEY = ''
    AND B.CORRECTED = ''
    AND B.TSTAMP >= SN.TSTAMP - 10 DAYS
    INNER JOIN AF.PROS V
    ON SN.NUMBER = V.NUMBER
    INNER JOIN AF.CHARGES H
    ON V.QUOTE_ID = H.QUOTE_ID
    AND H.STATUS = 'X'
    LEFT OUTER JOIN AF.SPELLING C
    ON B.CUST = C.ACCT_NUM
    AND C.TYPE = 'M'

    WHERE WEB_ID IN ('XXXXXXX','XXXXXXX','XXXXXXX')
    AND SN.TSTAMP > TIMESTAMP(CHAR(CURRENT DATE - 7 DAYS)||'-00.00.00.000000')

) T
WHERE T.RN = 1
ORDER BY T.PICKUP_TSTAMP
于 2013-10-04T20:11:45.497 回答
0

尝试通过使用公用表表达式 [CTE] 在两个概念查询步骤中解决此问题。首先像以前一样获取查询结果,但按最新(最高)修订的顺序对行进行排名。然后仅从该 CTE 中选择最新的行。

完成此操作后,您可能会将一些连接移至第二部分。我已经尝试为您演示移动一个,但您可能可以移动更多。

WITH q as
(SELECT RTRIM(CUST_REF_NUMBER) AS "PICKUP #"
       ,RTRIM(SN.NUMBER)       AS "NUMBER"
       ,DATE(PICKUP_TSTAMP)    AS "PICKUP DATE"
       ,B.DUE_DATE
       ,WEIGHT
       ,TOTAL_PIECES           AS "PIECES"
       ,H.QUOTE_ID             AS "QUOTE"
       ,EQUIP                  AS "ACTUAL QUOTE PRICE"
       ,TOTAL_CHG              AS "CHARGES"
       ,B.CUST                 AS "ACCOUNT NUMBER"
       ,H.REVISION
       ,ROW_NUMBER() OVER (PARTITION BY  H.QUOTE_ID 
                           ORDER BY      H.REVISION DESC)  AS AGEING
  FROM AF.NOTIF SN
  JOIN AF.BILL B
                       ON SN.NUMBER = B.NUMBER
                      AND B.ARCHIVE_KEY = ''
                      AND B.CORRECTED = ''
                      AND B.TSTAMP >= SN.TSTAMP - 10 DAYS
  JOIN AF.PROS V
                       ON SN.NUMBER = V.NUMBER
  JOIN AF.CHARGES H
                       ON H.QUOTE_ID = V.QUOTE_ID
                      AND H.STATUS = 'X'
  WHERE WEB_ID IN ('XXXXXXX','XXXXXXX','XXXXXXX')
    AND SN.TSTAMP > TIMESTAMP(CHAR(CURRENT DATE - 7 DAYS)||'-00.00.00.000000')
 )
 SELECT
       "PICKUP #"
      ,"NUMBER"
      ,"PICKUP DATE"
      , DUE_DATE
      , WEIGHT
      ,"PIECES"
      ,"QUOTE"
      ,"ACTUAL QUOTE PRICE"
      ,"CHARGES"
      ,"ACCOUNT NUMBER"
      ,C.ACCT_SPELLING        AS "ACCOUNT NAME"
      , REVISION
  FROM q
  LEFT OUTER
  JOIN AF.SPELLING C
                       ON B.CUST = C.ACCT_NUM
                      AND C.TYPE = 'M'
  WHERE AGEING = 1
  ORDER BY PICKUP_DATE
于 2013-10-05T21:22:38.520 回答