简体   繁体   English

C#WPF SQL)如何以编程方式知道SQL数据库表中特定日期的最新日期(用户输入记录的地方)或最大值?

[英]C# WPF SQL) How to know latest date(where user inputed a record) or biggest value in specific month in SQL database table programmatically?

This is not about last day of specific month. 这与特定月份的最后一天无关。

I'm trying to calculate CAGR ratio (compound annual(month) growth rate). 我正在尝试计算CAGR比率(复合年(月)增长率)。 So, at the last inputed date, there's always biggest value in the specific month meaning the value only increase. 因此,在最后输入的日期中,特定月份中始终会有最大值,这意味着该值只会增加。

However, there's possibility that users don't input everyday(including last day of month). 但是,用户有可能每天不输入(包括每月的最后一天)。 Therefore, I have to know which date is what user inputed most lately in the specific month. 因此,我必须知道哪个日期是用户在特定月份最近输入的时间。

Please refer to attached image. 请参考所附图片。

在此处输入图片说明

I've been always happy with excellence of people in stackoverflow ! 我一直对Stackoverflow的优秀人才感到满意! Thank you so much ! 非常感谢 !

Here is the syntax for SQL Server: 这是SQL Server的语法:

WITH
CTE
AS
(
    SELECT 
        dt
        ,Value
        ,ROW_NUMBER() 
        OVER (PARTITION BY DATEDIFF(month, '2001-01-01', dt) ORDER BY dt desc) AS rn
    FROM YourTable
)
SELECT
    dt
    ,Value
FROM CTE
WHERE rn = 1
;

In general, look up top-n-per-group or greatest-n-per-group . 通常,查找top-n-per-grouptop-n-per-group greatest-n-per-group

For a more detailed answer with other variants how to do it see: https://dba.stackexchange.com/questions/86415/retrieving-n-rows-per-group 有关其他变体的更详细答案,请参见: https : //dba.stackexchange.com/questions/86415/retrieving-n-rows-per-group

SELECT top(1) * 
FROM table 
WHERE dt >= 01/02/2016 
    AND dt <= 29/02/2016 
ORDER BY value DESC

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

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