在没有比赛的情况下加入多对一

时间:2019-03-27 16:24:44

标签: sql oracle

我不确定我的问题的措词是否正确,但总之,我需要4个表来访问数据。深度,深度,深度和深度。我需要在输出中显示每个DEPTHSET,每个DEPTH 1行,并使用LISTAGG函数汇总DEPTHENTRY中的数据。我的问题是,如果没有通过DEPTHSETMM许多映射表与之绑定的有效DEPTHENTRY,则不会为每个DEPTH都获得一行。

我已经通过with子句提供了示例数据,然后在其下面的实际代码以及当前输出和所需的输出。

WITH
DEPTH AS (
(SELECT 1 AS "DEPTHID", 'Group'  AS "NAME" FROM DUAL) UNION
(SELECT 2 AS "DEPTHID", 'Branch' AS "NAME" FROM DUAL) UNION
(SELECT 3 AS "DEPTHID", 'Area'   AS "NAME" FROM DUAL) UNION
(SELECT 4 AS "DEPTHID", 'Dept'   AS "NAME" FROM DUAL) UNION
(SELECT 5 AS "DEPTHID", 'Shift'  AS "NAME" FROM DUAL) UNION
(SELECT 6 AS "DEPTHID", 'Rpt'    AS "NAME" FROM DUAL) UNION
(SELECT 7 AS "DEPTHID", 'Code'   AS "NAME" FROM DUAL)
),
DEPTHSET AS (
(SELECT 3705 AS "DEPTHSETID", 'Idaho Set' AS "NAME" FROM DUAL)
),
DEPTHSETMM AS (
(SELECT 3705 AS "DEPTHSETID", 1410 AS "ENTRYID" FROM DUAL) UNION
(SELECT 3705 AS "DEPTHSETID", 1420 AS "ENTRYID" FROM DUAL) UNION
(SELECT 3705 AS "DEPTHSETID", 1421 AS "ENTRYID" FROM DUAL) UNION
(SELECT 3705 AS "DEPTHSETID", 1430 AS "ENTRYID" FROM DUAL) 
),
DEPTHENTRY AS (
(SELECT 1410 AS "ENTRYID", 'North West' AS "NAME", 1 AS "DEPTHID" FROM DUAL) UNION
(SELECT 1420 AS "ENTRYID", 'Zone 1' AS "NAME", 3 AS "DEPTHID" FROM DUAL) UNION
(SELECT 1421 AS "ENTRYID", 'Zone 2' AS "NAME", 3 AS "DEPTHID" FROM DUAL) UNION
(SELECT 1430 AS "ENTRYID", 'A' AS "NAME", 7 AS "DEPTHID" FROM DUAL) 
)
SELECT
DST.name AS "DEPTH_SET_NAME",
DEP.depthid AS "DEPTHID",
DEP.name AS "DEPTH_NAME",
LISTAGG(CAST(DEE.name AS varchar2(2000)), '; ') WITHIN GROUP (ORDER BY DEE.name DESC) AS "ENTRY_NAME"
FROM DEPTHSETMM DMM
  LEFT OUTER JOIN DEPTHENTRY DEE ON (DMM.entryid = DEE.entryid)
  LEFT OUTER JOIN DEPTH DEP      ON (DEE.depthid = DEP.depthid)
  LEFT OUTER JOIN DEPTHSET DST        ON (DMM.depthsetid = DST.depthsetid)
GROUP BY DST.name, DEP.depthid, DEP.name
  ORDER BY DST.name, DEP.depthid

电流输出

DEPTH_SET_NAME  DEPTHID  DEPTH_NAME  ENTRY_NAME
Idaho Set       1        Group       North West
Idaho Set       3        Area        Zone 2; Zone 1
Idaho Set       7        Code        A

所需的输出

DEPTH_SET_NAME  DEPTHID  DEPTH_NAME  ENTRY_NAME
Idaho Set       1        Group       North West
Idaho Set       2        Branch      NULL
Idaho Set       3        Area        Zone 2; Zone 1
Idaho Set       4        Dept        NULL
Idaho Set       5        Shift       NULL
Idaho Set       6        Rpt         NULL
Idaho Set       7        Code        A

1 个答案:

答案 0 :(得分:1)

您可以在DEPTHSETDEPTH之间交叉联接,然后外部联接其余表:

WITH ...
SELECT
DST.name AS "DEPTH_SET_NAME",
DEP.depthid AS "DEPTHID",
DEP.name AS "DEPTH_NAME",
LISTAGG(CAST(DEE.name AS varchar2(2000)), '; ') WITHIN GROUP (ORDER BY DEE.name DESC) AS "ENTRY_NAME"
FROM DEPTHSET DST
CROSS JOIN DEPTH DEP
  LEFT OUTER JOIN DEPTHENTRY DEE ON (DEE.depthid = DEP.depthid)
  LEFT OUTER JOIN DEPTHSETMM DMM ON (DMM.entryid = DEE.entryid)
                                 AND (DMM.depthsetid = DST.depthsetid)
GROUP BY DST.name, DEP.depthid, DEP.name
  ORDER BY DST.name, DEP.depthid;

DEPTH_SET    DEPTHID DEPTH_ ENTRY_NAME                    
--------- ---------- ------ ------------------------------
Idaho Set          1 Group  North West                    
Idaho Set          2 Branch                               
Idaho Set          3 Area   Zone 2; Zone 1                
Idaho Set          4 Dept                                 
Idaho Set          5 Shift                                
Idaho Set          6 Rpt                                  
Idaho Set          7 Code   A                             

请注意第二个外部联接中的AND (DMM.depthsetid = DST.depthsetid),取代了以前的间接条件。