0

我的脚本带​​回重复名称时遇到问题,我不确定如何解决。我的脚本将列连接成一行。基本上,我需要在分配给他们的建筑物的同一行和列中列出我需要的管理员名称。管理员名称被多次列出,当我连接行时,它列出了管理员名称,如下所示(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                  

期望的结果: 在此处输入图像描述

4

1 回答 1

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,

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

于 2013-09-28T21:09:50.550 回答