I am working in Greenplum - postgresql DB and have below structure of data:
Table Data - Screenshot
In this I need below logic to implement (some of which I already implemented):
CASE WHEN PDATE IS NOT NULL THEN to_char(PDATE,'YYYY-MM-DD')
WHEN PDATE IS NULL THEN to_char(NDATE,'YYYY-MM-DD N')
WHEN NDATEIS NULL THEN 'NO PO' ELSE 'NO PO' END
According to which I need QTY and VName.QTY: Sum(Qty) according to min (PDATE and NDATE)VName: VName according to min (PDATE and NDATE)
I need below data from raw data:
Need Data
as far I have made below query:
SELECT
ITEM ,
MIN(CASE WHEN PDATE IS NOT NULL THEN to_char(PDATE,'YYYY-MM-DD')
WHEN PDATE IS NULL THEN to_char(NDATE,'YYYY-MM-DD N')
WHEN NDATE IS NULL THEN 'NO PO' ELSE 'NO PO' END) AS PRO
FROM
Table
GROUP BY
ITEM
Please help me out with the query


Check Solution