我寫了一個查詢,讓我從2015年估計的增長 - 2016年SQL - 使用的值,直到結束,然後用最後的值
我使用一個名爲EBIT變量,EBIT與Date_Year = 2015有數據對於Date_Month 2013-12-31至2016-12-31,EBIT With Date_Year = 2016的數據爲Date_Month 2013-12-31至2017-06-30。
有沒有辦法像我一樣做exakt,但在2016-12-31之後使用EBIT(2015)的最後一個值代替,因此繼續?
在下面的代碼中,EBIT(2015)& EBIT(2016)僅用於說明數字。 最下面的表格是我想要獲得的最佳表格,但我無法理解如何到達那裏......
正如您所看到的,當前輸出在2016-12-31停止,而期望輸出假設EBIT(2015)= 936808,並且EBIT(2016)繼續執行,直到數據結束。
我想也許包含類似下面的代碼的東西,但我不能有一個最大功能在一個總和(案件時),我也認爲這可能不會選擇最大n2.EBIT爲最大(Date_Month_Id )用相同Date_Year_ID等
(
SUM(case when n2.Date_Month_Id < (Select Max(Date_Month_Id) From EBIT where Date_Year_ID in (2015))
Then n2.EBIT else max(n2.EBIT) end
)
/
SUM(n1.EBIT) - 1) AS 'EBIT Growth 2015-2016'
當前查詢:
SELECT
m1.date_Month,
SUM(n2.EBIT) As 'EBIT (2015)',
SUM(n1.EBIT) AS 'EBIT (2016)',
SUM(n2.EBIT)/
SUM(n1.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 = 2015 AND y2.date_year = 2016
GROUP BY m1.date_month
ORDER BY m1.Date_Month ASC;
電流輸出:
date_Month EBIT (2015) EBIT (2016) EBIT Growth 2015-2016
2013-12-31 2198051.670 2053514.548 0.070385
2014-01-31 1112047.668 1045523.283 0.063627
2014-02-28 1109221.007 1043085.024 0.063404
2014-03-31 1118112.429 1047602.842 0.067305
2014-04-30 1117082.270 1044455.198 0.069535
2014-05-31 1122019.824 1045228.639 0.073468
2014-06-30 1130536.274 1053516.159 0.073107
2014-07-31 1135838.665 1055882.605 0.075724
2014-08-31 1138886.938 1056331.236 0.078153
2014-09-30 1147685.937 1064254.322 0.078394
2014-10-31 1126437.959 1040798.042 0.082282
2014-11-30 1116292.468 1029127.634 0.084697
2014-12-31 1111509.880 1020233.204 0.089466
2015-01-31 1109692.378 1011895.933 0.096646
2015-02-28 1103494.875 1000990.365 0.102403
2015-03-31 1099507.246 996252.094 0.103643
2015-04-30 1094694.816 997653.412 0.097269
2015-05-31 1103352.777 1007836.533 0.094773
2015-06-30 1098103.559 1004344.223 0.093353
2015-07-31 1081689.371 994391.939 0.087789
2015-08-31 1064033.692 979809.245 0.085960
2015-09-30 1041604.341 971746.514 0.071888
2015-10-31 1044583.652 979410.989 0.066542
2015-11-30 1049158.666 989746.574 0.060027
2015-12-31 1022646.632 969556.360 0.054757
2016-01-31 990592.876 968797.454 0.022497
2016-02-29 961009.086 934777.852 0.028061
2016-03-31 942917.628 933858.404 0.009700
2016-04-30 937784.980 931500.111 0.006747
2016-05-31 941049.211 928974.727 0.012997
2016-06-30 930969.603 929102.681 0.002009
2016-07-31 926670.277 928979.675 -0.002486
2016-08-31 927442.570 929233.754 -0.001928
2016-09-30 924658.701 930248.793 -0.006010
2016-10-31 925569.857 930250.547 -0.005032
2016-11-30 938894.794 930156.559 0.009394
2016-12-31 936808.419 929592.092 0.007762
所需的輸出(和%增加,但我知道如何實現代碼):
date_month EBIT(2015) EBIT(2016)
31/12/2013 2198052 2053515
31/01/2014 1112048 1045523
28/02/2014 1109221 1043085
31/03/2014 1118112 1047603
30/04/2014 1117082 1044455
31/05/2014 1122020 1045229
30/06/2014 1130536 1053516
31/07/2014 1135839 1055883
31/08/2014 1138887 1056331
30/09/2014 1147686 1064254
31/10/2014 1126438 1040798
30/11/2014 1116292 1029128
31/12/2014 1111510 1020233
31/01/2015 1109692 1011896
28/02/2015 1103495 1000990
31/03/2015 1099507 996252
30/04/2015 1094695 997653
31/05/2015 1103353 1007837
30/06/2015 1098104 1004344
31/07/2015 1081689 994392
31/08/2015 1064034 979809
30/09/2015 1041604 971747
31/10/2015 1044584 979411
30/11/2015 1049159 989747
31/12/2015 1022647 969556
31/01/2016 990593 968797
29/02/2016 961009 934778
31/03/2016 942918 933858
30/04/2016 937785 931500
31/05/2016 941049 928975
30/06/2016 930970 929103
31/07/2016 926670 928980
31/08/2016 927443 929234
30/09/2016 924659 930249
31/10/2016 925570 930251
30/11/2016 938895 930157
31/12/2016 936808 929592
31/01/2017 936808 942461
28/02/2017 936808 936845
31/03/2017 936808 940401
30/04/2017 936808 933644
31/05/2017 936808 942218
詩篇。我使用的是Microsoft SQL Server Management Studio中
----編輯---------
感謝幫助我已經能夠得到下面的代碼能產生幾乎完全是我需要。然而,如果我保留「AND y1.date_year = y2.date_Year + 1」條件,那麼我將無法獲得n2.EBIT過去2017-01-31的值。如果刪除它,查詢就會因爲某種原因總結出許多值。 .. ......你知道的任何變通
重做代碼:
SELECT
m1.date_Month,
isnull(sum(case when y2.date_year = 2015 then n2.EBIT end),Max(innern2.december_value)) as 'EBIT 2015',
sum(case when y1.date_year = 2016 then n1.EBIT end) AS 'EBIT (2016)',
sum(case when y1.date_year = 2016 then n1.EBIT end)/
isnull(sum(case when y2.date_year = 2015 then n2.EBIT end),SUM(innern2.december_value)) - 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
LEFT 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
(
SELECT maxn2.date_year_id, SUM(maxn2.EBIT) as december_value
FROM EBIT maxn2
Inner join Date_Month As M on M.Date_Month_Id = maxn2.Date_Month_Id
inner join Date_Year as Y on Y.Date_Year_Id = maxn2.Date_Year_Id
WHERE Month(Date_Month) = 12 and year(Date_Month) = 2016
GROUP BY maxn2.date_year_id
) as innern2 on innern2.date_year_id = n1.date_year_id - 1
WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 AND y1.date_year = y2.date_year + 1
and n2.Date_Month_Id >= (Select Min(E.Date_Month_Id) from EBIT as E inner join Date_Year as Y on Y.Date_Year_Id = E.Date_Year_Id Where Y.Date_Year = 2016)
GROUP BY m1.date_month
ORDER BY m1.Date_Month asc;
輸出與約束提到:
date_Month EBIT 2015 EBIT (2016)
2013-12-31 2053514.548 2198051.670
2014-01-31 1045523.283 1112047.668
2014-02-28 1043085.024 1109221.007
2014-03-31 1047602.842 1118112.429
2014-04-30 1044455.198 1117082.270
2014-05-31 1045228.639 1122019.824
2014-06-30 1053516.159 1130536.274
2014-07-31 1055882.605 1135838.665
2014-08-31 1056331.236 1138886.938
2014-09-30 1064254.322 1147685.937
2014-10-31 1040798.042 1126437.959
2014-11-30 1029127.634 1116292.468
2014-12-31 1020233.204 1111509.880
2015-01-31 1011895.933 1109692.378
2015-02-28 1000990.365 1103494.875
2015-03-31 996252.094 1099507.246
2015-04-30 997653.412 1094694.816
2015-05-31 1007836.533 1103352.777
2015-06-30 1004344.223 1098103.559
2015-07-31 994391.939 1081689.371
2015-08-31 979809.245 1064033.692
2015-09-30 971746.514 1041604.341
2015-10-31 979410.989 1044583.652
2015-11-30 989746.574 1049158.666
2015-12-31 969556.360 1022646.632
2016-01-31 968797.454 990592.876
2016-02-29 934777.852 961009.086
2016-03-31 933858.404 942917.628
2016-04-30 931500.111 937784.980
2016-05-31 928974.727 941049.211
2016-06-30 929102.681 930969.603
2016-07-31 928979.675 926670.277
2016-08-31 929233.754 927442.570
2016-09-30 930248.793 924658.701
2016-10-31 930250.547 925569.857
2016-11-30 930156.559 938894.794
2016-12-31 929592.092 936808.419
2017-01-31 942617.388 NULL
2017-02-28 942617.388 NULL
2017-03-31 942617.388 NULL
2017-04-30 942617.388 NULL
2017-05-31 942617.388 NULL
輸出與建議約束「和y1.date_Year = 2016 「:
date_Month EBIT 2015 EBIT (2016)
2013-12-31 2053514.548 8781104.520
2014-01-31 1045523.283 3330912.804
2014-02-28 1043085.024 3322433.491
2014-03-31 1047602.842 3349059.127
2014-04-30 1044455.198 3346078.340
2014-05-31 1045228.639 3360889.672
2014-06-30 1053516.159 3385242.192
2014-07-31 1055882.605 3401244.625
2014-08-31 1056331.236 3410616.294
2014-09-30 1064254.322 3436856.111
2014-10-31 1040798.042 3373162.267
2014-11-30 1029127.634 3342757.174
2014-12-31 1020233.204 4363552.086
2015-01-31 1011895.933 4384691.285
2015-02-28 1000990.365 4376807.747
2015-03-31 996252.094 4375725.598
2015-04-30 997653.412 4357711.561
2015-05-31 1007836.533 4392712.780
2015-06-30 1004344.223 4373955.988
2015-07-31 994391.939 4314820.563
2015-08-31 979809.245 4247412.535
2015-09-30 971746.514 4151358.468
2015-10-31 979410.989 4161422.299
2015-11-30 989746.574 4179141.525
2015-12-31 969556.360 4077629.132
2016-01-31 968797.454 2970088.034
2016-02-29 934777.852 2881375.640
2016-03-31 933858.404 2827941.356
2016-04-30 931500.111 2815130.846
2016-05-31 928974.727 2824951.723
2016-06-30 929102.681 2794527.987
2016-07-31 928979.675 2783688.491
2016-08-31 929233.754 2785958.962
2016-09-30 930248.793 2783949.407
2016-10-31 930250.547 2787387.285
2016-11-30 930156.559 2827684.912
2016-12-31 929592.092 2822043.195
2017-01-31 930177.105 1884922.522
2017-02-28 930177.105 1873690.578
2017-03-31 930177.105 1880802.312
2017-04-30 930177.105 1867287.280
2017-05-31 930177.105 1884436.230
輸出而不「和y1.date_year = y2.date_year - 1個約束:
date_Month EBIT 2015 EBIT (2016)
2013-12-31 4255200.364 8781104.520
2014-01-31 3237430.013 3330912.804
2014-02-28 3201517.162 3322433.491
2014-03-31 3190328.126 3349059.127
2014-04-30 3176902.028 3346078.340
2014-05-31 3171643.267 3360889.672
2014-06-30 3172452.363 3385242.192
2014-07-31 3169541.043 3401244.625
2014-08-31 3170477.020 3410616.294
2014-09-30 3192395.112 3436856.111
2014-10-31 3120825.630 3373162.267
2014-11-30 3082544.372 3342757.174
2014-12-31 4002614.551 4363552.086
2015-01-31 3997360.080 4384691.285
2015-02-28 3968359.858 4376807.747
2015-03-31 3965754.869 4375725.598
2015-04-30 3971742.802 4357711.561
2015-05-31 4010919.518 4392712.780
2015-06-30 3997415.185 4373955.988
2015-07-31 3966592.901 4314820.563
2015-08-31 3911382.409 4247412.535
2015-09-30 3871093.275 4151358.468
2015-10-31 3900314.386 4161422.299
2015-11-30 3938511.747 4179141.525
2015-12-31 3862780.776 4077629.132
2016-01-31 2903039.552 2970088.034
2016-02-29 2801085.898 2881375.640
2016-03-31 2798222.402 2827941.356
2016-04-30 2794500.333 2815130.846
2016-05-31 2787967.181 2824951.723
2016-06-30 2788351.043 2794527.987
2016-07-31 2787982.025 2783688.491
2016-08-31 2788744.262 2785958.962
2016-09-30 2790746.379 2783949.407
2016-10-31 2790751.641 2787387.285
2016-11-30 2790469.677 2827684.912
2016-12-31 2789361.289 2822043.195
2017-01-31 942617.388 1884922.522
2017-02-28 942617.388 1873690.578
2017-03-31 942617.388 1880802.312
2017-04-30 942617.388 1867287.280
2017-05-31 942617.388 1884436.230
感謝您的幫助。我已經混合了y1和y2 ... 我之前嘗試過一個左連接,但是由於我的WHERE子句刪除了所有y2_date_year <> 2015中的值,Null值不會顯示出來。我認爲這個代碼可以工作,如果 1)我可以在某種程度上改變看看y2.date_year = 2015的位置,直到我們結束,然後考慮到最後一個值。 和2)如果我可以重寫代碼以允許936808號碼是動態的(給定y2.date_year的最後一個值)而不是常量。 更多提示? –
@CedricVongheer好的 - 考慮加入作爲內聯表查詢。請參閱編輯以回答。對不起,它需要你自己做一些解釋,但我假設你的表具有確切的數據結構。 – rayzinnz
感謝您的協助,請參閱--- EDIT--原文。 tl:dr - > 錯誤表示我需要對innern2.december_value進行分組或集合,以便獲得任何我已經放入sum(innern2.deceber_value)的輸出,但此值爲ofc。到高。問題2是我沒有從EBIT(2016)中獲得價值,可能是因爲哪裏的caluse,因爲如果我刪除了約束條件,我確實會得到值,但一切都會變形。 –