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

时间:2017-05-29 03:21:16

标签: mysql sql

我们有一个YTD报告,每月运行一次。我想通过计算月+ 1减去Month0 = MTD来计算每个月的MTD运动

以下表格中的数据

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

我有很多帐户,我需要像下面这样运行MTD。

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

我读过你可以做一个可以在同一个表中搜索不同行的自联接。但是如果你有3个月以上的话,我不能完全理解你怎么做多次。让Jan和Feb看起来很直接。

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]

以下结果

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

我想做一个我每月可以运行的查询。因此,例如,当9月份到来时,它会给我9行MTD。一月至九月基本上它将在当前表中可用的所有月份运行。感谢。

2 个答案:

答案 0 :(得分:1)

上述一个小的补充。

基于与YTD-1进行比较并且包括MTD的情况的前提,尽管1月在示例中得到解决但是它不起作用,例如当数据仅记录在3月时。第一个MTD将为NULL。

所以我只是在没有以前的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

答案 1 :(得分:0)

您可以尝试以下查询

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]