DB Query for Pivot

hi team

With below results in Db2 v10.5 . Please refer column A and B are same,while Staus column defers with distinct values .

A B STATUS
Insert Update Old
Insert Update New
Insert Update Final

Can someone guide how to achieve this
A B Status
Insert Update Old
Null or Blank New
Null or Blank Final

Column A and B should be show same value . The o/p values are dynamic. please guide

Usually, sql implementations come with some formatting features. Here, I'd suggest a "break column". Specify like break on A on B mayhap with a skip n to highlight a value change in A or B .

Not sure if I understood the "o/p values are dynamic" part, but see if this helps:

SQL>
SQL> SELECT * FROM mytable;

A          B          STATUS
---------- ---------- ----------
Insert     Update     Old
Insert     Update     New
Insert     Update     Final

3 rows selected.

SQL>
SQL> -- Using CASE expression
SQL> SELECT CASE status WHEN 'Old' THEN a ELSE NULL END AS a,
  2         CASE status WHEN 'Old' THEN b ELSE NULL END AS b,
  3         status
  4    FROM mytable
  5  ;

A          B          STATUS
---------- ---------- ----------
Insert     Update     Old
                      New
                      Final

3 rows selected.

SQL>
SQL> -- Using DECODE function
SQL> SELECT DECODE(status, 'Old', a, NULL) AS a,
  2         DECODE(status, 'Old', b, NULL) AS b,
  3         status
  4    FROM mytable
  5  ;

A          B          STATUS
---------- ---------- ----------
Insert     Update     Old
                      New
                      Final

3 rows selected.

SQL>
SQL>
1 Like