简体   繁体   English

SQL-根据YTD编号计算得出的MTD编号

[英]SQL - MTD numbers calculated from YTD numbers

we have a YTD report which is run on a monthly basis. 我们有一个YTD报告,该报告每月运行一次。 I would like to calculate the MTD movement for each month by doing a calculation of Month+1 minus Month0 = MTD 我想通过计算Month + 1减去Month0 = MTD来计算每个月的MTD运动

Data as per below in one table 下表中的数据如下

Month | Account | YTD amount
Jan   | AB123   | 100
Feb   | AB123   | 200
Mar   | AB123   | 350

I have many accounts which I would need a running MTD like below. 我有许多帐户,需要运行如下所示的MTD。

Month | Account | MTD Amount
Jan   | AB123   | 100
Feb   | AB123   | 100
Mar   | AB123   | 150

I read you can do a self join which could search for a different line within the same table. 我读过您可以做一个自我联接,可以在同一张表中搜索另一行。 But I can't quite get how you could do this multiple times if you have 3+ months. 但是,如果您有超过3个月的时间,我无法完全理解如何多次执行此操作。 Having Jan and Feb looks straight forward. 一月和二月看起来直截了当。

SELECT A.[Month]
  ,A.[Account]
  ,A.[YTD] FebYTD
  ,B.[YTD] JanYTD
  ,A.[YTD] - B.[YTD] MTD
FROM 
(
SELECT [Month]
   ,[Account]
   ,[YTD]
FROM [PWC_2017].[dbo].[MTD test]
where [Month] = 'Feb'
) A
join
(
SELECT [Month]
  ,[Account]
  ,[YTD]
FROM [PWC_2017].[dbo].[MTD test]
Where [Month] = 'Jan'
) B
on A.[Account] = B.[Account]

Results below 结果如下

Month | Account | FebYTD | JanYTD | MTD
Feb   | AB123   |   200  |  100   | 100

I'd like to do one query which I could run each month. 我想做一个查询,每个月可以运行一次。 So for example, when September month comes around, it's going to give me 9 rows of MTD. 因此,例如,当9月一个月来临时,它将给我9行MTD。 Jan-Sep. 1月-9月。 Basically it will run on all the months available in the table at the current time. 基本上,它将在当前时间表中可用的所有月份上运行。 Thanks. 谢谢。

one minor addition to the above. 上述内容的一个小补充。

Based on premise of comparing to YTD- 1 and having included a case for the MTD, although the January is solved for the example it doesn't work eg when data is only recorded ao March. 基于与YTD-1进行比较的前提,并包括了MTD的情况,尽管示例中解决了1月的问题,但例如仅在3月左右记录数据时,它是行不通的。 The first MTD will be NULL. 第一个MTD将为NULL。

So I simply, added a zero when no previous YTD exists, 所以我简单地讲,当以前的YTD不存在时,添加一个零,

CASE CurrentYTD.Month_ID
WHEN 1 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0) 
WHEN 2 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 3 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 4 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 5 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 6 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 7 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 8 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 9 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 10 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 11 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
WHEN 12 Then COALESCE (CurrentYTD.Amount,PreviousYTD.Amount)- COALESCE(PreviousYTD.Amount,0)
END AS Amount_MTD

You could try the following query 您可以尝试以下查询

SELECT Current.[Month], 
       Current.[Account], 
       Current.[YTD]                                       CurrentYTD, 
       Next.[YTD]                                          NextYTD, 
       COALESCE(Next.[YTD], Current.[YTD]) - Current.[YTD] MTD 
FROM   (SELECT [Month], 
               CASE [Month] 
                 WHEN 'Jan' THEN 1 
                 WHEN 'Feb' THEN 2 
                 WHEN 'Mar' THEN 3 
                 WHEN 'Apr' THEN 4 
                 WHEN 'May' THEN 5 
                 WHEN 'June' THEN 6 
                 WHEN 'July' THEN 7 
                 WHEN 'Aug' THEN 8 
                 WHEN 'Sept' THEN 9 
                 WHEN 'Oct' THEN 10 
                 WHEN 'Nov' THEN 11 
                 WHEN 'Dec' THEN 12 
               END AS [MonthId], 
               [Account], 
               [YTD] 
        FROM   [PWC_2017].[dbo].[MTD test]) Current 
       LEFT JOIN (SELECT [Month], 
                         CASE [Month] 
                           WHEN 'Jan' THEN 1 
                           WHEN 'Feb' THEN 2 
                           WHEN 'Mar' THEN 3 
                           WHEN 'Apr' THEN 4 
                           WHEN 'May' THEN 5 
                           WHEN 'June' THEN 6 
                           WHEN 'July' THEN 7 
                           WHEN 'Aug' THEN 8 
                           WHEN 'Sept' THEN 9 
                           WHEN 'Oct' THEN 10 
                           WHEN 'Nov' THEN 11 
                           WHEN 'Dec' THEN 12 
                         END AS [MonthId], 
                         [Account], 
                         [YTD] 
                  FROM   [PWC_2017].[dbo].[MTD test]) Next 
              ON Current.[Account] = Next.[Account] 
                 And Current.[MonthId] + 1 = Next.[MonthId] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM