消除递归SQL重复

时间:2013-09-27 16:27:22

标签: sql db2 duplicates recursive-query

我的脚本有问题带回重复的名称,我不知道如何修复它。我的脚本将列连接成一行。基本上我有我需要的管理员名称列在他们被分配的建筑物的同一行和列中。管理员名称被多次列出,当我连接行时,它列出了管理员名称,如下所示(John Doe | John Doe)。联系人和列表有时包含不同的名称。我需要在列表中排除重复的名称。我附上了当前和期望结果的屏幕截图。使用db2数据库但不确定版本。感谢您的帮助。

       WITH
    /*****************************************************
    *** The cte was used to generate test data easily. ***
    *****************************************************/
    sample_data
( rownum, project_id , project_name ,  name_last , name_first, point_of_contact, building_id, building_name, hours_used,
hours_to_use, percentage_used, capability, bucket_a, bucket_b, bucket_c  ) AS
(
VALUES
  (1, 10 , 'ELITE', 'Gamb', 'Susan', 'Susan Gamb | Amber Jones', 2, 'Main' , 80, 0, 1.0, 6000, 12000, 9600, 5100)
, (2, 10 , 'ELITE', 'Gamb', 'Susan', 'Susan Gamb | Amber Jones', 2 , 'Main', 80,  0, 1.0, 7000, 12500, 8000, 4000)
, (3, 10 , 'ELITE', 'Jones', 'Amber', 'Susan Gamb | Amber Jones', 2, 'Main', 80, 0, 1.0, 6000, 12000, 9600, 5100)
, (4, 10 , 'ELITE', 'Jones', 'Amber', 'Susan Gamb | Amber Jones', 2, 'Main', 80, 0, 1.0, 7000, 12500, 8000, 4000)
, (1, 10 , 'ELITE', 'Gamb', 'Susan', 'Susan Gamb | Amber Jones', 6 ,'Warehouse', 40, 40, .5, 6000, 12500, 9600, 5100)
, (2, 10 , 'ELITE', 'Gamb', 'Susan', 'Susan Gamb | Amber Jones', 6, 'Warehouse' , 40, 40, .5, 7000, 12000, 8000, 4000)
, (3, 10 , 'ELITE', 'Jones', 'Amber',  'Susan Gamb | Amber Jones', 6, 'Warehouse' , 40, 40, .5, 6000, 12500, 9600, 5100)
, (4, 10 , 'ELITE', 'Jones', 'Amber',  'Susan Gamb | Amber Jones', 6, 'Warehouse' , 40, 40, .5, 6000, 12000, 8000, 4000 )
, (1, 1040 , 'ROADRUNNER', 'Sugar', 'Paul', 'Paul Sugar | Rob Brown', 2 , 'Main', 60,  20, .75, 5000, 1000, 1200, 4100 )
, (2, 1040 , 'ROADRUNNER', 'Sugar', 'Paul', 'Paul Sugar | Rob Brown', 2, 'Main',  60, 20, .75, 4000, 1500, 1000, 3000)
, (3, 1040 , 'ROADRUNNER', 'Brown', 'Rob', 'Paul Sugar | Rob Brown', 2, 'Main', 60, 20, .75, 4500, 2000, 1200, 4100)
, (4, 1040 , 'ROADRUNNER', 'Brown', 'Rob', 'Paul Sugar | Rob Brown', 2, 'Main', 60, 20, .75, 4000, 1500, 1000, 3000)
, (1, 1040 , 'ROADRUNNER', 'Sugar', 'Paul', 'Paul Sugar | Rob Brown',  6 , 'Warehouse', 60, 20, .75, 4500, 2000, 1200, 4100)
, (2, 1040 , 'ROADRUNNER', 'Sugar', 'Paul', 'Paul Sugar | Rob Brown',  6, 'Warehouse', 60, 20, .75, 4000, 1500, 1000, 3000)
, (2, 1040 , 'ROADRUNNER',  'Brown', 'Rob', 'Paul Sugar | Rob Brown',  6, 'Warehouse', 60, 20, .75, 4500, 2000, 1200, 4100 )
, (3, 1040 , 'ROADRUNNER',  'Brown', 'Rob', 'Paul Sugar | Rob Brown',  6, 'Warehouse', 60, 20, .75, 4000, 1500, 1000, 3000)

)
    ,

           t2(PROJECT_ID, LIST, POINT_OF_CONTACT, PROJECT_NAME, BUILDING_ID, BUILDING_NAME, HOURS_USED, HOURS_TO_USE, PERCENTAGE_USED, CAPABILITY,
           BUCKET_A, BUCKET_B, BUCKET_C, cnt) AS
        ( SELECT    PROJECT_ID,
                    VARCHAR(NAME_FIRST CONCAT ' ' CONCAT NAME_LAST, 6000),
                    POINT_OF_CONTACT,
                    PROJECT_NAME,
                    BUILDING_ID,
                    BUILDING_NAME,
                    HOURS_USED,
                    HOURS_TO_USE,
                    PERCENTAGE_USED,
                    CAPABILITY,
                    BUCKET_A,
                    BUCKET_B,
                    BUCKET_C,
                    1
        FROM SAMPLE_DATA
                WHERE rowNum = 1
                UNION ALL
        SELECT
            t2.PROJECT_ID,
                    t2.list || ' | ' || SAMPLE_DATA.NAME_FIRST CONCAT ' ' CONCAT SAMPLE_DATA.NAME_LAST,
                    SAMPLE_DATA.POINT_OF_CONTACT,
                    SAMPLE_DATA.PROJECT_NAME,
                    SAMPLE_DATA.BUILDING_ID,
                    SAMPLE_DATA.BUILDING_NAME,
                    SAMPLE_DATA.HOURS_USED,
                    SAMPLE_DATA.HOURS_TO_USE,
                    SAMPLE_DATA.PERCENTAGE_USED,
                    SAMPLE_DATA.CAPABILITY,
                    SAMPLE_DATA.BUCKET_A,
                    SAMPLE_DATA.BUCKET_B,
                    SAMPLE_DATA.BUCKET_C,
                    t2.cnt + 1
        FROM t2, SAMPLE_DATA

                WHERE t2.PROJECT_ID = SAMPLE_DATA.PROJECT_ID
                AND          t2.BUILDING_ID = SAMPLE_DATA.BUILDING_ID
                AND   t2.cnt + 1 = SAMPLE_DATA.rowNum
                 )    
        SELECT
                   PROJECT_ID,
                   PROJECT_NAME,
                   POINT_OF_CONTACT,
                   BUILDING_ID,
                   BUILDING_NAME,
                   HOURS_USED,
                   HOURS_TO_USE,
                   PERCENTAGE_USED,
                   CAPABILITY,
                   BUCKET_A,
                   BUCKET_B,
                   BUCKET_C,
                   list
        FROM t2
                WHERE ( PROJECT_ID, BUILDING_ID, cnt ) IN (
        SELECT PROJECT_ID, BUILDING_ID, MAX(rowNum)
        FROM SAMPLE_DATA
                GROUP BY PROJECT_ID, BUILDING_ID )    
                order by PROJECT_NAME                  

期望的结果: enter image description here

1 个答案:

答案 0 :(得分:1)

我能够在MSSQL服务器中测试您的查询并使其正常工作。我尽力在db2中查找等效函数。即使这不能直接发挥作用,我希望它能让你走上正确的道路。

在第二次选择t2时,我改变了:

t2.list || ' | ' || SAMPLE_DATA.NAME_FIRST CONCAT ' ' CONCAT SAMPLE_DATA.NAME_LAST,

        WHEN LOCATE(SAMPLE_DATA.NAME_FIRST CONCAT ' ' CONCAT SAMPLE_DATA.NAME_LAST, t2.list) > 0 THEN t2.list
        ELSE t2.list || ' | ' || SAMPLE_DATA.NAME_FIRST || ' ' || SAMPLE_DATA.NAME_LAST
        END,

基本上,我们正在检查该人是否已经在列表中,如果是,则返回当前列表,如果不是,则将其添加到列表中。