0

我想按两列(PROJECT_ID 和 CATEGORY_NAME)进行分区,但我无法编写正确的语法。我下面的查询是有效的,但是当我尝试添加一个额外的 over 子句时,它不能正常工作。递归查询用于连接在 project_id 上分区的行,创建一个管理员列表,组合并连接 name_last 和 name_first 以形成一个列表。我需要使用额外的 over 子句来包含 CATEGORY_NAME,因为列表中的管理员在不同类别(“INVISION”和“INSIGHT”)中工作,但在相同的 project_id 下。第一个子查询

SELECT 
        RowNumber() over (PARTITION BY F13.DIM_PROJECT_ID, F13.CATEGORY_NAME ORDER BY F13.PROJECT_NAME),      
        F13.DIM_PROJECT_ID.....etc.

提取正确的数据,我只是不确定如何按项目和类别将正确的数据拉出分区。我正在使用 db2。

with

   t1(rowNum, PROJECT_ID, NAME_LAST, NAME_FIRST, POINT_OF_CONTACT, PROJECT_NAME, BUSINESS_NAME) as 

   (
      SELECT 
        RowNumber() over (PARTITION BY F13.DIM_PROJECT_ID, F13.CATEGORY_NAME ORDER BY F13.PROJECT_NAME),      
        F13.DIM_PROJECT_ID,
        F2P.NAME_LAST,
        F2P.NAME_FIRST, 
        REPLACE(F2P.POINT_OF_CONTACT, ',', ' |') AS POINT_OF_CONTACT,
        F13.PROJECT_NAME,
        F2H.CATEGORY_NAME,

FROM FACT_TABLE AS F13
INNER JOIN ADMIN AS F2P ON F13.DIM_PROJECT_ID = F2P.DIM_PROJECT_ID
LEFT JOIN HOURS AS F2H ON F13.DIM_PROJECT_ID = F2H.DIM_PROJECT_ID
WHERE F2H.CATEGORY_NAME = ('INVISION')




group by  
        F13.DIM_PROJECT_ID,
        F13.PROJECT_NAME,
        F2P.NAME_LAST,
        F2P.NAME_FIRST, 
        F2P.POINT_OF_CONTACT,
        F2H.CATEGORY_NAME
 ) ,

       t2(PROJECT_ID, LIST, POINT_OF_CONTACT, PROJECT_NAME, BUSINESS_NAME, cnt) AS
    ( SELECT    PROJECT_ID, 
                VARCHAR(NAME_FIRST CONCAT ' ' CONCAT NAME_LAST, 6000),
                POINT_OF_CONTACT, 
                PROJECT_NAME, 
                CATEGORY_NAME,
                1
    FROM t1
            WHERE rowNum = 1
            UNION ALL
    SELECT      t2.PROJECT_ID,
                t2.list || ' | ' || t1.NAME_FIRST CONCAT ' ' CONCAT t1.NAME_LAST, 
                t1.POINT_OF_CONTACT,
                t1.PROJECT_NAME, 
                t1.CATEGORY_NAME
    FROM t2, t1 
            WHERE t2.project_id = t1.project_id 
            AND   t2.cnt + 1 = t1.rowNum )    
    SELECT      PROJECT_ID, 
                PROJECT_NAME, 
                POINT_OF_CONTACT,
                CATEGORY_NAME
                list
    FROM t2
            WHERE ( PROJECT_ID, cnt ) IN (
    SELECT PROJECT_ID, MAX(rowNum)
    FROM t1
            GROUP BY PROJECT_ID )    

我得到的结果是产生重复的,但只有当第二列 (category_name 包含在分区子句中时。当前结果: 在此处输入图像描述

期望的结果:

在此处输入图像描述

4

1 回答 1

1

我想到了。我为类别添加了一个 ID,并按 category_id 和 project_id 进行了分区。

with

   t1(rowNum, PROJECT_ID, NAME_LAST, NAME_FIRST, POINT_OF_CONTACT, PROJECT_NAME, CATEGORY_ID, CATEGORY_NAME) as 

   (
      SELECT 
        RowNumber() over (PARTITION BY F13.DIM_PROJECT_ID, F13.CATEGORY_ID ORDER BY F13.PROJECT_NAME, F13.CATEGORY_NAME),      
        F13.DIM_PROJECT_ID,
        F2P.NAME_LAST,
        F2P.NAME_FIRST, 
        REPLACE(F2P.POINT_OF_CONTACT, ',', ' |') AS POINT_OF_CONTACT,
        F13.PROJECT_NAME,
        F13.CATEGORY_ID
        F13.CATEGORY_NAME,

FROM FACT_TABLE AS F13
INNER JOIN ADMIN AS F2P ON F13.DIM_PROJECT_ID = F2P.DIM_PROJECT_ID
LEFT JOIN HOURS AS F2H ON F13.DIM_PROJECT_ID = F2H.DIM_PROJECT_ID
WHERE F13.CATEGORY_NAME = ('INVISION')




group by  
        F13.DIM_PROJECT_ID,
        F13.PROJECT_NAME,
        F2P.NAME_LAST,
        F2P.NAME_FIRST, 
        F2P.POINT_OF_CONTACT,
        F13.CATEGORY_ID
        F13.CATEGORY_NAME
 ) ,

       t2(PROJECT_ID, LIST, POINT_OF_CONTACT, PROJECT_NAME, CATEGORY_ID, CATEGORY_NAME, cnt) AS
    ( SELECT    PROJECT_ID, 
                VARCHAR(NAME_FIRST CONCAT ' ' CONCAT NAME_LAST, 6000),
                POINT_OF_CONTACT, 
                PROJECT_NAME, 
                CATEGORY_ID,
                CATEGORY_NAME,
                1
    FROM t1
            WHERE rowNum = 1
            UNION ALL
    SELECT      t2.PROJECT_ID,
                t2.list || ' | ' || t1.NAME_FIRST CONCAT ' ' CONCAT t1.NAME_LAST, 
                t1.POINT_OF_CONTACT,
                t1.PROJECT_NAME, 
                t1.CATEGORY_ID,
                t1.CATEGORY_NAME
    FROM t2, t1 
            WHERE t2.project_id = t1.project_id 
            AND t2.category_id = t1.category_id
            AND   t2.cnt + 1 = t1.rowNum )    
    SELECT      PROJECT_ID, 
                PROJECT_NAME, 
                POINT_OF_CONTACT,
                CATEGORY_ID,
                CATEGORY_NAME
                list
    FROM t2
            WHERE ( PROJECT_ID, CATEGORY_ID, cnt ) IN (
    SELECT PROJECT_ID, CATEGORY_ID, MAX(rowNum)
    FROM t1
            GROUP BY PROJECT_NAME ) 
于 2013-09-17T21:23:43.870 回答