Can someone help me to achieve below mentioned requirement in postgresql.
Input table:
Col1 Col2 Col3 Col4
Jan XXX YYY 1234
Jan XXX YYY 5264
Jan XXX YYY 4736
Feb ZZZ WWW 123
Feb ZZZ WWW 456
Output text file: ( Delimited)
Col1 Col2 Col3 Col4
Jan XXX YYY 1234
NULL NULL NULL 5264
NULL NULL NULL 4736
Feb ZZZ WWW 123
NULL NULL NULL 456
I want like this column
2
Answers
You can do:
However, formatting should be done at the UI level, not at the query level. I would strongly suggest you do it in the UI.
To achieve the desired output in PostgreSQL, you can use the LAG function to compare the current row with the previous row and check if the values of Col1, Col2 and Col3 are the same. If they are different, then you can set the values to NULL for Col2 and Col3.
Here’s an example query that should achieve the desired output:
In this query, the LAG function is used to compare the current row with the previous row in the order of Col1 and Col4. If the values of Col1, Col2, and Col3 are the same as the previous row, then NULL values are assigned to Col2 and Col3. Otherwise, the values of Col1, Col2, Col3, and Col4 are returned as is.
This should give you the desired output in the format you specified.