简体   繁体   English

如何比较当前月值与最近12个月的值

[英]How to compare the current month value with last 12 months

I'm trying to compare the current month value with the last 12 months values. 我正在尝试将当前月份的值与最近12个月的值进行比较。

E.g:
6/30/2019 100
5/31/2019 90
4/30/2019 80
3/31/2019 70
2/8/2019 60
1/31/2019 50
12/31/2018 40
11/30/2018 30
10/31/2018 20
9/30/2018 10
8/31/2018 90
7/30/2018 110

Now the current month value(6/30/2019) in 100 then I want to compare these value with the last 12 months values. 现在当前月份值(6/30/2019)为100,那么我想将这些值与最近12个月的值进行比较。 If the current month value is the maximum when compared with the 12 months then I want to set the flag as "max". 如果当前的月份值与12个月相比是最大值,那么我想将标记设置为“最大值”。 In the above example, 110 is the maximum but current month 100 ie, it is minimum when compared with the last 12 months values then I want to set the flag as "min". 在上面的示例中,110是最大值,但当前月份是100,即与最近12个月的值相比是最小值,那么我想将标记设置为“ min”。

Also, I want to get the date also ie, if it is minimum then what is the maximum value with the date (expected output "MIN(110 as on 7/30/2018)"). 此外,我还想获取日期,即如果它是最小值,那么日期是最大值(预期输出为``MIN(2018年7月30日为110)'')。

Please provide me any solution to achieve this scenario 请提供给我解决方案的任何解决方案

expected output "MIN(110 as on 7/30/2018)" 预期输出“ MIN(2018年7月30日为110)”

If I understand correctly, here's one way to do it: 如果我理解正确,这是一种解决方法:

-- Create a table variable to hold the sample data
DECLARE @data TABLE (
    [Date] DATE,
    [Value] INT
)

-- Load the sample data table
DECLARE @i INT = 0;
WHILE @i < 12
BEGIN
    INSERT INTO @data ([Date], [Value]) values (EOMONTH(DATEADD(M,-@i, GETDATE())), FLOOR(RAND()*(100-1+1))+10);
    SET @i = @i + 1;
END;

-- Select the resulting data set with flags
WITH t as
(
SELECT [Date], [Value], 
    CASE 
        WHEN [Value] = (SELECT MAX([Value]) FROM @data) THEN 'MAX' 
        WHEN [Value] = (SELECT MIN([Value]) FROM @data) THEN 'MIN' 
        ELSE '' 
    END AS Flag 
FROM @data
)
SELECT 'MAX(' + CAST([Value] as VARCHAR(MAX)) + ' as on ' + CAST([Date] as VARCHAR(MAX)) + ')' FROM t WHERE [Flag] = 'MAX'

This will output: 这将输出:

MAX(109 as on 2018-09-30)

Swap "MIN" for "MAX" and you'll have the minimum value. 将“ MIN”交换为“ MAX”,您将获得最小值。

If you want to compare the current value to a rolling 12-month min/max, you can use window functions: 如果要将当前值与连续12个月的最小值/最大值进行比较,可以使用窗口函数:

select top (1) t.*,
       (case when value = max12_value then 'MAX'
             when value = min12_value then 'MIN'
        end) as flag
from (select t.*,
             max(value) over (order by date rows between 11 preceding and current row) as max12_value,
             min(value) over (order by date rows between 11 preceding and current row) as min12_value,
      from t
     ) t
order by date desc

This following script will give you output considering the current month value always calculated from GETDATE() 以下脚本将为您提供考虑始终根据GETDATE()计算的当前月份值的输出

WITH CTE (d, value)
AS
(
    SELECT id,value FROM your_table
),
CTE2
AS
(
    SELECT DISTINCT
    (SELECT Value FROM CTE WHERE d>= CAST(DATEADD(DD,-DATEPART(DD,GETDATE()) + 1,GETDATE())  AS DATE)) current_month_value,
    MIN(value) min_value,
    MAX(value) max_value
    FROM CTE
    WHERE d <  CAST(DATEADD(DD,-DATEPART(DD,GETDATE()) + 1,GETDATE())  AS DATE)
    AND d >= CAST(DATEADD(MM,-13,DATEADD(DD,-DATEPART(DD,GETDATE()) + 1,GETDATE()) ) AS DATE)
)

SELECT 
CASE 
    WHEN  current_month_value > max_value THEN 'MAX'
    ELSE 'MIN(' + CAST(max_value AS VARCHAR)+ ' AS ON '+ (SELECT TOP 1 CAST(d AS VARCHAR) FROM CTE WHERE Value = max_value)+ ')'
END
FROM CTE2

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

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