我需要一个CASE表达式吗?

时间:2019-12-06 18:57:45

标签: sql sql-server case

我有以下查询,该查询返回上一年度的组织(从本年度开始,因此为2018年)的总工资。

SELECT 
    organization_id, 
    CASE 
       WHEN organization_id IN (SELECT text_1 
                                FROM combo_table_detail 
                                WHERE combo_table_id = 'wageAdjustment') 
          THEN SUM(ISNULL(component_value, 0)) + ISNULL(ctd.number_2, 0) 
          ELSE SUM(ISNULL(component_value, 0)) 
    END AS "total_annual_wage", 
    MAX((begin_date)) AS "total_annual_wage_eff_date" 
FROM   
    actual_pay_hours aph 
LEFT JOIN 
    combo_table_detail ctd ON aph.organization_id = ctd.text_1 
                           AND combo_table_id = 'wageAdjustment' 
WHERE  
    organization_id = 'Org1' 
    AND component_name IN ('earnDef', 'earnings') 
    AND begin_date >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) 
    AND begin_date < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
GROUP BY 
    organization_id, ctd.number_2 

但是,我遇到了一个问题:一些组织要么没有上一年的工资(有些组织只有2019年的工资),要么它们的最新工资是2014年的工资。在两种情况下,查询都返回空白值。这是由于

AND begin_date >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) 
AND begin_date < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)

预期结果应如下所示:

+-----------------+-------------------+----------------------------+
| organization_id | total_annual_wage | total_annual_wage_eff_date |
+-----------------+-------------------+----------------------------+
| Org1            |             50000 | 12/1/2018                  |
+-----------------+-------------------+----------------------------+

但是,它看起来像这样:

+-----------------+-------------------+----------------------------+
| organization_id | total_annual_wage | total_annual_wage_eff_date |
+-----------------+-------------------+----------------------------+

这个问题似乎是一个事实,在aph表中,有些单位有2018年的工资,而其他单位没有。示例:

SELECT DISTINCT
    YEAR(BEGIN_DATE) AS [Begin Date for Org1]
FROM    
    ACTUAL_PAY_HOURS aph
WHERE
    ORGANIZATION_ID = 'Org1'

结果:

+---------------------+
| Begin Date for Org1 |
+---------------------+
|                1988 |
|                1989 |
|                1990 |
|                1991 |
|                1992 |
|                1993 |
|                1994 |
|                2004 |
|                2005 |
|                2006 |
|                2007 |
|                2008 |
|                2009 |
|                2010 |
|                2011 |
|                2012 |
|                2013 |
|                2014 |
+---------------------+

此外,然后通过CTD(combo_table_detail)表中的值来调整上一年的总工资。问题是,当一个存在年份没有任何值时,什么也不会返回。我需要的是将工资总额设为0-因为该年没有任何数据,但是要添加CTD表中的值。

因此,如果Org1没有2018年的工资,它应该像这样出来:

+-----------------+-------------+-------------------+-------+
| organization_id | total_wages | combo_table_value | total |
+-----------------+-------------+-------------------+-------+
| Org1            |           0 |             25000 | 25000 |
+-----------------+-------------+-------------------+-------+

所以我的问题是,我可以在此查询中添加什么逻辑,以便在本组织没有上一年度的工资时返回结果,但仍将其添加到CTD表中,从而返回值? / p>

2 个答案:

答案 0 :(得分:1)

假设WHERE子句中的两个字段属于联接表。
尝试将这些条件移至“左联接”。

FromStr

答案 1 :(得分:0)

SELECT 
    aph.organization_id,
    MAX(aph.begin_date) AS total_annual_wage_eff_date, 
    --earn components sum
    ISNULL( SUM(aph.component_value), 0)
    +
    --adjustment sum, if any
    (
    SELECT ISNULL(SUM(ctd.number_2), 0) 
    FROM combo_table_detail ctd 
    WHERE ctd.text_1 = aph.organization_id
      AND ctd.combo_table_id = 'wageAdjustment'
    ) AS total_annual_wage
FROM actual_pay_hours AS aph
WHERE aph.component_name IN ( 'earnDef', 'earnings' ) 
AND aph.begin_date >= DATEFROMPARTS(YEAR(GETDATE())-1, 1, 1)
AND aph.begin_date < DATEFROMPARTS(YEAR(GETDATE()), 1, 1)
GROUP BY aph.organization_id