0

假设我有一个返回结果如下的查询:

Project Year Type Amt
PJ00001 2012 1    1000
PJ00001 2012 2    1000
PJ00001 2011 1    1000
PJ00002 2012 1    1000

我想要什么:每个项目每年都会有 2 行类型。如果该行不存在,则将其添加到 Amt = 0 的结果中
。例如:
- PJ00001 在 2012 年有 2 行类型为 1,2 --> OK。但在 2011 年,它只有 1 行类型 1 --> 我们添加一行:PJ00001 2011 2 0
- PJ00002 只有 1 行类型 1 --> 添加:PJ00002 2012 2 0

有没有办法轻松做到。我现在知道的唯一方法是创建一个类似的视图:PJ_VIEW。接着:

SELECT *
FROM PJ_VIEW
UNION ALL
SELECT t.PROJECT, t.YEAR_NO, 1 AS TYPE_NO, 0 AS AMT
FROM PJ_VIEW t
WHERE NOT EXISTS (SELECT 1 FROM PJ_VIEW t2 WHERE t2.PROJECT = t.PROJECT AND t2.YEAR_NO = t.YEAR_NO AND t2.TYPE_NO = 1)
UNION ALL
SELECT t.PROJECT, t.YEAR_NO, 2 AS TYPE_NO, 0 AS AMT
FROM PJ_VIEW t
WHERE NOT EXISTS (SELECT 1 FROM PJ_VIEW t2 WHERE t2.PROJECT = t.PROJECT AND t2.YEAR_NO = t.YEAR_NO AND t2.TYPE_NO = 2)
4

2 回答 2

2

对于基于数据库的解决方案,您需要一个 CROSS JOIN。以下解决方案适用于n 个项目类型。此解决方案需要一个包含所有具有默认金额的项目类型的表。

首先创建你的表:

    CREATE TABLE `_projects` (
      `Project`     VARCHAR(20) NOT NULL,
      `_Year`       INT(4) NOT NULL,
      `ProjectType` INT(1) NOT NULL,
      `ProjectAmt`  INT(11) NOT NULL
    );

    CREATE TABLE `_projecttypes` (
      `DefaultType` INT(1) NOT NULL,
      `DefaultAmt`  INT(11) DEFAULT 0
    );

插入您的项目名称、年份、项目类型和金额:

    INSERT INTO `_projects`
    VALUES ('PJ00001', 2012, 1,    1000),
           ('PJ00001', 2012, 2,    1000),
           ('PJ00001', 2011, 1,    1000),
           ('PJ00002', 2012, 1,    1000);

现在使用默认值 0(零)或您需要的任何值添加您的项目类型:

    INSERT INTO `_projecttypes`
    VALUES (1,0),(2,0);

这是将默认值 0 添加到任何还没有默认值的项目的结果中的查询。

    SELECT 
        IFNULL(`_actualprojects`.`Project`,`der2`.`Project`)          AS `_Project`,
        IFNULL(`_actualprojects`.`_Year`,`der2`.`_Year`)              AS `_Year`,
        IFNULL(`_actualprojects`.`ProjectType`,`der2`.`DefaultType`)  AS `_Type`,
        IFNULL(`_actualprojects`.`ProjectAmt`,`der2`.`DefaultAmt`)    AS `_Amt` 
    FROM 

        (   SELECT DISTINCT `der1`.`DefaultType`,
                            `der1`.`Project`,
                            `der1`.`_Year`,
                            `der1`.`DefaultAmt` 
            FROM 
                (   SELECT *
                    FROM `_projecttypes` CROSS JOIN  `_projects` 
                ) AS `der1`
        ) AS `der2` 

        LEFT JOIN `_projects` AS `_actualprojects` 
            ON `der2`.`Project`       = `_actualprojects`.`Project`
            AND `der2`.`_Year`        = `_actualprojects`.`_Year`
            AND `der2`.`DefaultType`  = `_actualprojects`.`ProjectType`
    ;

如果您想要更多项目类型,您可以简单地将它们添加到具有默认数量的 _projecttype 表中,然后重新执行上述查询。

试试看,添加这些值,然后重新运行上述查询。

    INSERT INTO `_projecttypes`
    VALUES (3,0),(4,0);

我希望这可以帮助你。

于 2012-11-07T04:53:45.293 回答
1

使用 DB2 临时表尝试类似的操作。只需将名为 query 的表的内容替换为您的查询即可。

WITH TypeList AS ( 
    SELECT 1 TypeCode
    UNION ALL
    SELECT 2
),
query AS (
    SELECT Project, [Year], [Type], Amt FROM yourQuerySource
),
DistinctProjectYears AS (
    SELECT Project, [YEAR]
    FROM query
    GROUP BY Project, [YEAR]
),
Projects AS (
    SELECT Project, [YEAR], [Type] as TypeCode, MAX(Amt) as Amt
    FROM query
    GROUP BY Project, [Year], [Type]
)
SELECT l.TypeCode, py.Project, py.[Year], COALESCE(t.Amt, 0) as Amt
FROM TypeList l CROSS JOIN DistinctProjectYears py
    LEFT JOIN Projects t ON l.TypeCode = t.TypeCode
        AND py.Project = t.Project
        AND py.[Year] = t.[Year]
于 2012-11-07T04:58:11.100 回答