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