需要找到最小和最大日期列表標籤 像這樣在裏面一列敏日期:2015年1月1日/最大日期:2015年1月5日SQL LISTAGG最小和最大功能問題
SELECT D.ITEM_ID AS "ItemId",
C.NAME AS "ItemName",
D.UOM_ID AS "UomId",
B.DESCRIPTION AS "Uom",
sum(D.REQUIRED_QTY) AS "Quantity",
LISTAGG(A.REASON, ', ') WITHIN GROUP (ORDER BY A.REASON) "ReasonType",
LISTAGG(D.REQUIRED_DATE, ', ') WITHIN GROUP (ORDER BY D.REQUIRED_DATE) "PoRequiredDate",
LISTAGG(D.PO_COMMENT, ', ') WITHIN GROUP (ORDER BY D.PO_COMMENT) "Comment"
FROM BIZZXE_V2_SCH.REASONS A,
BIZZXE_V2_SCH.UOMS B,
BIZZXE_V2_SCH.ITEMS C,
BIZZXE_V2_SCH.PO_REQUEST_ITEMS D,
BIZZXE_V2_SCH.PO_REQUESTS E
WHERE E.PO_REQUEST_ID=D.PO_REQUEST_ID
AND D.ITEM_ID=C.ITEM_ID
AND D.UOM_ID=B.UOM_ID
AND D.REASON_ID=A.REASON_ID
GROUP BY D.ITEM_ID, C.NAME, D.UOM_ID, B.DESCRIPTION
需要DT加民dt和最高一列作爲「PoRequiredDate」
WITH DATA AS(
SELECT listagg(REQUIRED_DATE, ',') WITHIN GROUP (ORDER BY REQUIRED_DATE) dt
FROM PO_REQUEST_ITEMS
WHERE PO_REQUEST_ID =16
)
SELECT dt,
SUBSTR(dt, 1, instr(dt, ',', 1, 1)-1) min_dt,
SUBSTR(dt, instr(dt, ',', -1, 1)+1) max_dt
FROM data
/
謝謝你這是我搜索 – Beez
添加更多細節並通過添加「NULLS FIRST」修復NULL值的可能錯誤。 – BitLord
謝謝你 – Beez