所以我有一个查询,我想抓住一个包扫描到HAZ位置的第一个事务时间,如下面的查询所示:
SELECT
LOCATION,
COUNT(PACKAGE_ID) OVER (PARTITION BY LOCATION) AS PACKAGE_COUNT,
PACKAGE_ID,
(TRUNC(SYSDATE)-TRUNC(DATE_RECEIVED)) AS DAYS_AGED,
ORDER_NUMBER
FROM
(SELECT
DISTINCT
LOCATION,
PACKAGE_ID,
ORDER_NUMBER,
FIRST_VALUE(TRAN_DATE) OVER (PARTITION BY PACKAGE_ID ORDER BY TRAN_DATE) AS DATE_RECEIVED
FROM
(SELECT
(CASE WHEN SPM.PARENT_PACKAGE_ID IS NULL THEN SPM.PACKAGE_ID ELSE SPM.PARENT_PACKAGE_ID END) AS PACKAGE_ID,
SUBSTR(SPM.ORDER_NUMBER,1,5) AS ORDER_NUMBER,
SPM.LOCATION AS LOCATION,
PL.TRAN_DATE AS TRAN_DATE
FROM SO_PACKAGE_MASTER SPM
INNER JOIN PACKAGE_LOG PL ON PL.PACKAGE_ID = SPM.PACKAGE_ID
WHERE SPM.LOCATION LIKE '%HAZ%'
AND PL.DESTINATION LIKE '%HAZ%'
AND SPM.SHIPPED = 'N'
AND SPM.STATUS = 'C'))
ORDER BY LOCATION, DAYS_AGED DESC, PACKAGE_ID;
我得到以下数据集:
HAZOUT-01 13 1894980 254 04591
HAZOUT-01 13 1895207 158 04591
HAZOUT-01 13 1510372 85 28236
HAZOUT-01 13 1510374 85 28236
HAZOUT-01 13 1510535 85 28237
HAZOUT-01 13 1510537 85 28237
HAZOUT-01 13 1812277 36 MSR00
HAZOUT-01 13 1816943 29 22062
HAZOUT-01 13 1816947 29 22062
HAZOUT-01 13 1852815 28 27677
HAZOUT-01 13 1855792 28 27764
HAZOUT-01 13 1902199 2 22065
HAZOUT-01 13 1902202 2 22065
但如果我在我的package_log表中查找1894980,则表明它没有显示254天:
1894980 HAZOUT-01 15-MAR-18
任何想法?(系统日期是当前日期EST)
答案 0 :(得分:0)
您不是要按包ID要求第一个值。如果这是您想要的,请将其包含在partition by
:
SELECT PL.PACKAGE_ID, PL.DESTINATION,
FIRST_VALUE(PL.TRAN_DATE) OVER (PARTITION BY PL.PACKAGE_ID ORDER BY PL.TRAN_DATE) AS DATE_RECEIVED
FROM SO_PACKAGE_MASTER SPM INNER JOIN
PACKAGE_LOG PL
ON PL.PACKAGE_ID = SPM.PACKAGE_ID
WHERE PL.DESTINATION LIKE '%HAZ%' AND
SPM.SHIPPED = 'N' AND
SPM.STATUS = 'C';
我不确定您是否也希望在DESTINATION
中使用PARTITION BY
。
您的查询返回DESTINATION
的第一个值。
顺便说一句,你也可以使用MIN()
来表示这个逻辑:
SELECT PL.PACKAGE_ID, PL.DESTINATION,
MIN(PL.TRAN_DATE) OVER (PARTITION BY PL.PACKAGE_ID) AS DATE_RECEIVED
FROM SO_PACKAGE_MASTER SPM INNER JOIN
PACKAGE_LOG PL
ON PL.PACKAGE_ID = SPM.PACKAGE_ID
WHERE PL.DESTINATION LIKE '%HAZ%' AND
SPM.SHIPPED = 'N' AND
SPM.STATUS = 'C';
我认为没有理由改用FIRST_VALUE()
。