I have this requirement that I need to attach a new column in select statement. It holds the disp_order of the parent level.
I currently have this sql statement
SELECT DISTINCT order_no, code, disp_order, lvl, description FROM tbl_pattern +----------+------+------------+-----+-------------+ | order_no | code | disp_order | lvl | description | +----------+------+------------+-----+-------------+ | RM001-01 | 1 | 0 | 1 | HK140904-1A | | RM001-01 | 1 | 1 | 2 | HK140904-1B | | RM001-01 | 1 | 2 | 3 | HK140904-1B | | RM001-01 | 1 | 3 | 4 | HK140904-1C | | RM001-01 | 1 | 4 | 5 | HK140904-1D | | RM001-01 | 1 | 5 | 2 | HK140904-1E | | RM001-01 | 1 | 6 | 3 | HK140904-1E | | RM001-01 | 1 | 7 | 3 | HK140904-1X | | RM001-01 | 1 | 8 | 4 | HK140904-1E | | RM001-01 | 1 | 9 | 5 | HK140904-1E | +----------+------+------------+-----+-------------+ parent column does not exist on table, but I want to get the disp_order of the level higher than the current record.
From the example table, the result should be like this:
+----------+------+------------+-----+-------------+--------+ | order_no | code | disp_order | lvl | description | parent | +----------+------+------------+-----+-------------+--------+ | RM001-01 | 1 | 0 | 1 | HK140904-1A | | | RM001-01 | 1 | 1 | 2 | HK140904-1B | 0 | | RM001-01 | 1 | 2 | 3 | HK140904-1B | 1 | | RM001-01 | 1 | 3 | 4 | HK140904-1C | 2 | | RM001-01 | 1 | 4 | 5 | HK140904-1D | 3 | | RM001-01 | 1 | 5 | 2 | HK140904-1E | 0 | | RM001-01 | 1 | 6 | 3 | HK140904-1E | 5 | | RM001-01 | 1 | 7 | 3 | HK140904-1X | 5 | | RM001-01 | 1 | 8 | 4 | HK140904-1E | 7 | | RM001-01 | 1 | 9 | 5 | HK140904-1E | 8 | +----------+------+------------+-----+-------------+--------+ Data representation through hierarchy of sample provided:
1 └2 └3 └4 └5 └2 └3 └3 └4 └5