SQL - 带有和的两个表的条​​件(大小写)

时间:2017-07-17 09:56:48

标签: sql sql-server case-when

我有一个代码可以让我接近我想要的答案。

现在,代码会在不同估计值的时间之间产生增长,并返回不同年份的非空值。然而,我只需要对基础公司在两个期间而不仅仅是其中一个期间具有价值的值进行求和。

 Select m.date_Month
    ,sum(case when y.date_year = 2016 then n.EBIT end) / sum(case when y.date_year = 2015 then n.EBIT end) - 1 as 'EBIT Growth 2015-2016'  
 from EBIT as n
    inner join date_year as y on y.date_year_id = n.date_year_id  
    inner join date_month as m on m.date_month_id = n.date_month_id 
where n.EBIT<> 0  and m.date_month > '2012-11-30' and m.date_month < '2017-01-31'
 group by m.date_month
 order by m.Date_Month asc;

我猜测有些东西需要在sum(case .....)语法或where子句中添加。

一个简短的例子来说明我想要的东西:

Company_Id  Date_Year  Date_Month  Ebit
1           2015       2014-12-30  50
1           2016       2014-12-30  52
2           2015       2014-12-30  60
3           2015       2014-12-30  40
3           2016       2014-12-30  43

现在代码查看上面的表并采用:(52 + 43)/(50 + 60 + 40)-1

我只需要:(52 + 43)/(50 + 40)-1

因此,此特定Date_Month的最终输出最好是:

Date_Month    EBIT Growth 2015-2016
.....         ......    
2014-12-30    0.0556
.....         ......

当前输出(以及代码的期望输出可以在下面看到,但是这些值是错误的,因为它们包括2015年存在但2016年不存在的值(反之亦然)。

date_Month  2014-2015   2015-2016
2012-12-31  0.025866    NULL
2013-01-31  0.043509    NULL
2013-02-28  0.048632    NULL
2013-03-31  1.120582    NULL
2013-04-30  0.076691    NULL
2013-05-31  0.072184    NULL
2013-06-30  0.078506    NULL
2013-07-31  0.088014    NULL
2013-08-31  0.084344    NULL
2013-09-30  0.088520    NULL
2013-10-31  0.090181    NULL
2013-11-30  0.091335    NULL
2013-12-31  0.094102    0.989785
2014-01-31  0.094030    0.011348
2014-02-28  0.096986    0.024370
2014-03-31  0.096916    0.040109
2014-04-30  0.098773    0.044166
2014-05-31  0.099726    0.051751
2014-06-30  0.099677    0.063474
2014-07-31  0.099865    0.071188
2014-08-31  0.101077    0.073944
2014-09-30  0.101028    0.075055
2014-10-31  0.090556    0.079508
2014-11-30  0.083493    0.083621
2014-12-31  0.070896    0.088688
2015-01-31  0.072573    0.095857
2015-02-28  0.077159    0.102403
2015-03-31  0.082023    0.102581
2015-04-30  0.076842    0.096223
2015-05-31  0.088918    0.094773
2015-06-30  0.084905    0.093353
2015-07-31  0.075418    0.086845
2015-08-31  0.061566    0.085217
2015-09-30  0.051807    0.071888
2015-10-31  0.060045    0.066542
2015-11-30  0.072266    0.061107
2015-12-31  0.050182    0.055806
2016-01-31  NULL            0.023453
2016-02-29  NULL            0.029018
2016-03-31  NULL          0.011684
2016-04-30  NULL            0.007700
2016-05-31  NULL           0.012831
2016-06-30  NULL           0.001756
2016-07-31  NULL          -0.001628
2016-08-31  NULL          -0.001095
2016-09-30  NULL         -0.000649
2016-10-31  NULL          0.000707
2016-11-30  NULL          0.015307
2016-12-31  NULL          0.013374

感谢输入,这是几乎完全符合我想要的代码,但它不包括上面例子中看到的'Null'值:

SELECT
    m1.date_Month,
    SUM(n2.EBIT) /
    SUM(n1.EBIT) - 1 AS 'EBIT Growth 2014-2015',
    SUM(n3.EBIT) /
    SUM(n2.EBIT) - 1 AS 'EBIT Growth 2015-2016'
FROM EBIT AS n1
   INNER JOIN date_year AS y1 ON y1.date_year_id = n1.date_year_id
   INNER JOIN date_month AS m1 ON m1.date_month_id = n1.date_month_id
INNER JOIN EBIT AS n2
   INNER JOIN date_year AS y2 ON y2.date_year_id = n2.date_year_id
   INNER JOIN date_month AS m2 ON m2.date_month_id = n2.date_month_id
ON n1.Company_Id = n2.company_Id AND m1.date_month = m2.date_month
INNER JOIN EBIT AS n3
   INNER JOIN date_year AS y3 ON y3.date_year_id = n3.date_year_id
   INNER JOIN date_month AS m3 ON m3.date_month_id = n3.date_month_id
ON n2.Company_Id = n3.company_Id AND m2.date_month = m3.date_month
WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 and n3.EBIT <> 0 AND y1.date_year = 2014 AND y2.date_year = 2015 and y3.date_year = 2016 
GROUP BY m1.date_month
ORDER BY m1.Date_Month ASC;

我正在使用Microsoft Management Studio。

1 个答案:

答案 0 :(得分:1)

考虑重写您的查询,以便您使用基于date_monthcompany_id的标准加入两个句点(2016年和2015年),这些标准应过滤其中company_id缺少SELECT m1.date_Month, SUM(n1.EBIT) / SUM(n2.EBIT) - 1 AS 'EBIT Growth 2015-2016' FROM EBIT AS n1 INNER JOIN date_year AS y1 ON y1.date_year_id = n1.date_year_id INNER JOIN date_month AS m1 ON m1.date_month_id = n1.date_month_id INNER JOIN EBIT AS n2 INNER JOIN date_year AS y2 ON y2.date_year_id = n2.date_year_id INNER JOIN date_month AS m2 ON m2.date_month_id = n2.date_month_id ON n1.Company_Id = n2.company_Id AND m1.date_month = m2.date_month WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 AND y1.date_year = 2016 AND y2.date_year = 2015 GROUP BY m1.date_month ORDER BY m1.Date_Month ASC; 的行两个时期。

company_id

如果每个monthLEFT JOIN有多个记录,则此解决方案会出现问题。

如果您需要包含NULL值,请使用INNER JOIN代替x/NULL。在这种情况下,您会在任何地方获得NULL值,其中指定公司的后续年份没有匹配记录,因为NULL/xSUM都等于NULL而... LEFT JOIN EBIT AS n2 ... LEFT JOIN EBIT AS n3 ... 函数忽略NULL值仅当所有求和值均为NULL时才等于NULL。因此,即使在接下来的几年中没有相关值,结果集中也会包含基准年的所有记录。

SELECT
    m1.date_Month,
    SUM(n1.EBIT) /
    SUM(n2.EBIT) - 1 AS 'EBIT Growth 2016-2015',
    SUM(n2.EBIT) /
    SUM(n3.EBIT) - 1 AS 'EBIT Growth 2015-2014'
FROM EBIT AS n1
   INNER JOIN date_year AS y1 ON y1.date_year_id = n1.date_year_id
   INNER JOIN date_month AS m1 ON m1.date_month_id = n1.date_month_id
INNER JOIN EBIT AS n2
   INNER JOIN date_year AS y2 ON y2.date_year_id = n2.date_year_id
   INNER JOIN date_month AS m2 ON m2.date_month_id = n2.date_month_id
ON n1.Company_Id = n2.company_Id AND m1.date_month = m2.date_month
LEFT JOIN EBIT AS n3
   INNER JOIN date_year AS y3 ON y3.date_year_id = n3.date_year_id
   INNER JOIN date_month AS m3 ON m3.date_month_id = n3.date_month_id
ON n2.Company_Id = n3.company_Id AND m2.date_month = m3.date_month
                                 AND y3.date_year = 2014
WHERE y1.date_year = 2016 AND y2.date_year = 2015  
GROUP BY m1.date_month
ORDER BY m1.Date_Month ASC;

如果需要,您还可以拥有第一个加入INNER和第二个LEFT,并在上一个相关年份(2016年)开始加入。在这种情况下,您将只看到2016-2015的相关比较以及2015-2014的比较,其中一些将为空。

这样的事情:

main()