我创建了一些示例表和数据来尝试LISTAGG
SELECT SALE_TITLE, addr_id, addr_name, addr_desc
, LISTAGG(SALES_ID, ',')
WITHIN GROUP (ORDER BY SALES_ID)
AS SALES_ID
FROM (select a.addr_id, a.addr_name, a.addr_desc,
b.SALES_ID, b.SALE_TITLE,
ROW_NUMBER () OVER (ORDER BY b.SALES_ID ) rn,
COUNT (*) OVER () cnt
from palm.adrss a, palm.sales b
where a.addr_id = b.addr_id)
GROUP BY SALE_TITLE, addr_id, addr_name, addr_desc;
当我运行上述查询时,我的结果集如下:
+-----------------+----------+-----------+--------------+-------------+
| 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 |
+-----------------+----------+-----------+--------------+-------------+
现在使用这个结果集我想写另一个查询来获得如下所示的结果集。在此角色,权利,后缀将是列名称,并将在其下面具有sale_id
+---------------+--------------+---------+----------------------+---------+
| 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 | |
+---------------+--------------+---------+----------------------+---------+
如果可能,请告诉我,并提供一些意见
提前致谢
答案 0 :(得分:0)
我看到你有样本数据的拼写错误,如果你修复它,你将得到你想要的输出。
update Your_Table
set ADDR_NAME='Gou'
where ADDR_NAME='Gouri'
您的查询应该是:
select t1.ADDR_NAME,t1.ADDR_DESC,
max(case when t1.SALE_TITLE='Role' then t2.SALES_ID end) 'Role',
max(case when t1.SALE_TITLE='Entitlement' then t2.SALES_ID end) 'Entitlement',
max(case when t1.SALE_TITLE='Suffix' then t2.SALES_ID end) 'Suffix'
from Your_Table t1
join Your_Table t2
on t1.ADDR_ID=t2.ADDR_ID and t1.SALE_TITLE=t2.SALE_TITLE
group by t1.ADDR_NAME,t1.ADDR_DESC
order by Role