关于查询结果中多行字段合并到一行上的SQL语句写法
SELECT
C.CUST_ID,C.CUST_NAME,S2.PVI,S3.PUI_NBR
FROM CUST C,SERV_SERVICE S1,SERV_PVI S2,SERV_PUI S3
WHERE C.CUST_ID=S1.CUST_ID
AND S1.SERV_SERVICE_ID=S2.SERV_SERVICE_ID
AND S1.SERV_SERVICE_ID=S3.SERV_SERVICE_ID
AND S1.STATE='S11'
AND C.CUST_NAME LIKE '韩永佳%';
结果是这样
我想让结果只显示成一行。把PUI_NBR分三列显示SQL怎么写?
------解决方案--------------------SELECT C.CUST_ID,C.CUST_NAME,S2.PVI, WM_CONCAT(S3.PUI_NBR) AS PUI_NBRS
FROM CUST C,SERV_SERVICE S1,SERV_PVI S2,SERV_PUI S3
WHERE C.CUST_ID=S1.CUST_ID
AND S1.SERV_SERVICE_ID=S2.SERV_SERVICE_ID
AND S1.SERV_SERVICE_ID=S3.SERV_SERVICE_ID
AND S1.STATE='S11' AND C.CUST_NAME LIKE '韩永佳%'
GROUP BY C.CUST_ID,C.CUST_NAME,S2.PVI;
------解决方案--------------------SELECT CUST_ID,CUST_NAME,PVI,
max(decode(r,1,PUI_NBR,null)) PUI_NBR1,
max(decode(r,2,PUI_NBR,null)) PUI_NBR2,
max(decode(r,3,PUI_NBR,null)) PUI_NBR3
from
(SELECT C.CUST_ID,C.CUST_NAME,S2.PVI,S3.PUI_NBR, row_number() over(partition by C.CUST_ID,C.CUST_NAME,S2.PVI order by S3.PUI_NBR ) r FROM CUST C,SERV_SERVICE S1,SERV_PVI S2,SERV_PUI S3
WHERE C.CUST_ID=S1.CUST_ID
AND S1.SERV_SERVICE_ID=S2.SERV_SERVICE_ID
AND S1.SERV_SERVICE_ID=S3.SERV_SERVICE_ID
AND S1.STATE='S11' AND C.CUST_NAME LIKE '韩永佳%')
group by CUST_ID,CUST_NAME,PVI;