我有以下3个表:
SHIFT_MASTER,PATTERN_MASTER,PATTERN_DETAILS
S_ID,P_ID,P_D_ID分别是SHIFT_MASTER,PATTERN_MASTER,PATTERN_DETAILS表的priamry键。
SHIFT_MASTER
S_ID | S_NUMBER| S_Name
---------------------------------
1 A MORNING
2 B AFTERNOON
3 C NIGHT
PATTERN_MASTER
P_ID | P_NAME
----------------
1 Pattern 1
2 Pattern 2
PATTERN_DETAILS
P_D_ID|P_ID | S_ID| ...
---------------------
1 1 1
2 1 2
3 1 3
4 1 2
5 1 1
6 2 3
7 2 2
8 2 1
9 2 3
我GOT OUTPUT AS
P_ID | S_ID
1 1,2,3,2,1
2 3,2,1,3
使用QUERY
SELECT PATTERN_DETAILS.P_ID "PATTERN",
LISTAGG(PATTERN_DETAILS.S_ID, ', ')
WITHIN GROUP (ORDER BY PATTERN_DETAILS.P_D_ID) "SHIFT"
FROM PATTERN_DETAILS
GROUP BY PATTERN_DETAILS.P_ID;
我想要的是什么
P_NAME | S_NUMBER
Pattern 1 A,B,C,B,A
Pattern 2 C,B,A,C
有什么建议吗?而不是P_ID我想显示模式名称而不是移位ID我想显示移位数。如何与listagg函数一起执行连接操作?
答案 0 :(得分:2)
你需要加入所有三个表才能得到这个,
SELECT pm.p_name "P_NAME",
listagg(sm.s_number, ', ') WITHIN GROUP (ORDER BY pd.p_d_id) "S_NUMBER"
FROM pattern_master pm,
pattern_details pd,
shift_master sm
WHERE sm.s_id= pd.s_id
AND pm.p_id = pd.p_id
GROUP BY pm.p_name;