2

我正在寻找基于 3 个不同条件(DB2 数据库)的项目计数。我需要计算所有项目 <= .10、>= .5 和 >= 1.00 (percentage_used),但只能按 dim_building_id 和 building_name 分组。当然,这个查询不会运行,因为它需要将 percent_used 添加到 group by。如何使用 percent_used 处理这 3 个条件?

SELECT
SUM(CAST(FTS.GROUP_A AS BIGINT)) AS GROUP_A,
        SUM(CAST(FTS.GROUP_B AS BIGINT)) AS GROUP_B,
        SUM(CAST(FTS.GROUP_C AS BIGINT)) AS GROUP_C,


                CASE WHEN FAT.PERCENTAGE_USED <= '0.10'
                THEN COUNT(*)
                END AS PROJECTS_L10,
                CASE WHEN FAT.PERCENTAGE_USED >= '0.50' 
                THEN COUNT(*)
                END AS PROJECTS_G50,
                CASE WHEN FAT.PERCENTAGE_USED >= '1.00' 
                THEN COUNT(*)
                END AS PROJECTS_G100,
        DAYS(DATE('2014-07-01')) - DAYS(CURRENT DATE)  AS DAYS_LEFT,
        (DAYS(DATE('2014-07-01')) - DAYS(CURRENT DATE))/7 AS WEEKS_LEFT, 
        DAYS(DATE('2013-12-31')) - DAYS(CURRENT DATE) AS DAYS_LEFT_YEAR      

FROM FACT_TABLE AS FAT
INNER JOIN GROUPS AS FTS ON FAT.DIM_PROJECT_ID = FTS.DIM_PROJECT_ID
GROUP BY FAT.DIM_BUILDING_ID, FAT.BUILDING_NAME;
4

2 回答 2

2

Instead of putting the aggregate as the THEN, wrap the CASE statement in an aggregate:

SELECT
SUM(CAST(FTS.GROUP_A AS BIGINT)) AS GROUP_A,
        SUM(CAST(FTS.GROUP_B AS BIGINT)) AS GROUP_B,
        SUM(CAST(FTS.GROUP_C AS BIGINT)) AS GROUP_C,  
        SUM(CASE WHEN FAT.PERCENTAGE_USED <= '0.10' THEN 1 ELSE 0 END) AS PROJECTS_L10,
        SUM(CASE WHEN FAT.PERCENTAGE_USED >= '0.50' THEN 1 ELSE 0 END) AS PROJECTS_G50,
        SUM(CASE WHEN FAT.PERCENTAGE_USED >= '1.00' THEN 1 ELSE 0 END) AS PROJECTS_G100,
        DAYS(DATE('2014-07-01')) - DAYS(CURRENT DATE)  AS DAYS_LEFT,
        (DAYS(DATE('2014-07-01')) - DAYS(CURRENT DATE))/7 AS WEEKS_LEFT, 
        DAYS(DATE('2013-12-31')) - DAYS(CURRENT DATE) AS DAYS_LEFT_YEAR      

FROM FACT_TABLE AS FAT
INNER JOIN GROUPS AS FTS ON FAT.DIM_PROJECT_ID = FTS.DIM_PROJECT_ID
GROUP BY FAT.DIM_BUILDING_ID, FAT.BUILDING_NAME;

The ELSE 0 isn't needed, but some like to see it in the query.

Alternatively this should work as well:

COUNT(CASE WHEN FAT.PERCENTAGE_USED >= '0.50' THEN 1 END) AS PROJECTS_G50,
于 2013-09-25T16:56:30.323 回答
2

我会做这样的事情:

select bn.building_name ,
       t.*
from ( select fat.building_id ,
              sum( case when fat.percentage_used <= 0.10                                then 1 else 0 end ) as group_a ,
              sum( case when fat.percentage_used >  0.10 and fat.percentage_used < 0.50 then 1 else 0 end ) as group_b ,
              sum( case when fat.percentage_used >= 0.50 and fat.percentage_used < 1.00 then 1 else 0 end ) as group_c ,
              sum( case when fat.percentage_used >= 1.00                                then 1 else 0 end ) as group_d ,
              sum( case when fat.percentage_used is null                                then 1 else 0 end ) as group_e
       from fact_table fat
       join groups     fts on tfs.dim_project_id = fat.dim_project_id
       group by fat.building_id
     ) t
join fact_table bn on bn.building_id = t.building_id
于 2013-09-25T17:14:27.167 回答