sql - Query to get data from a table as column names in another table -
i created sample tables , data try listagg
select sale_title, addr_id, addr_name, addr_desc , listagg(sales_id, ',') within group (order sales_id) sales_id (select a.addr_id, a.addr_name, a.addr_desc, b.sales_id, b.sale_title, row_number () on (order b.sales_id ) rn, count (*) on () cnt palm.adrss a, palm.sales b a.addr_id = b.addr_id) group sale_title, addr_id, addr_name, addr_desc;
when run above query result set following
+-----------------+----------+-----------+--------------+-------------+ | sale_title | addr_id | addr_name | addr_desc | sales_id | +-----------------+----------+-----------+--------------+-------------+ | role | 2 | saty | local test | 14,34 | | entitlement | 2 | saty | local test | 22,42 | | role | 3 | vasu | portal | 14,34 | | entitlement | 3 | vasu | portal | 22,42 | | role | 4 | sand | golden gate | 144,344 | | entitlement | 4 | sand | golden gate | 224,424 | | role | 5 | gou | data modeler | 144 | | suffix | 5 | gouri | data modeler | 224,424 | | entitlement | 5 | gou | data modeler | 344 | | role | 6 | mad | data analyst | 144 | | entitlement | 6 | mad | data analyst | 224,344,424 | +-----------------+----------+-----------+--------------+-------------+
now using result set want write query result set looks below. in role, entitlement, suffix column names , have sale_id's under them
+---------------+--------------+---------+----------------------+---------+ | addr_name | addr_desc | role | entitlement | suffix | +---------------+--------------+---------+----------------------+---------+ | saty | local test | 14,34 | 22,42 | | | vasu | portal | 14,34 | 22,42 | | | sand | golden gate | 144,344 | 224,424 | | | gou | data modeler | 144 | 344 | 224,424 | | mad | data analyst | 144 | 224,344,424 | | +---------------+--------------+---------+----------------------+---------+
please let me know if possible , provide me inputs
thanks in advance
i saw have typo on sample data, if fix desired output.
update your_table set addr_name='gou' addr_name='gouri'
your query should be:
select t1.addr_name,t1.addr_desc, max(case when t1.sale_title='role' t2.sales_id end) 'role', max(case when t1.sale_title='entitlement' t2.sales_id end) 'entitlement', max(case when t1.sale_title='suffix' t2.sales_id end) 'suffix' your_table t1 join your_table t2 on t1.addr_id=t2.addr_id , t1.sale_title=t2.sale_title group t1.addr_name,t1.addr_desc order role
Comments
Post a Comment