简体   繁体   English

SQL Server:获取日期范围和天数

[英]SQL Server : get date range and number of days

I have 4 columns (two columns filter) and one column date and one column flag.我有 4 列(两列过滤器)和一列日期和一列标志。 For a combination of column 1 and column 2, and the open flag, I want to find the open date, the close date and number of days where it was open.对于第 1 列和第 2 列以及打开标志的组合,我想找到打开日期、关闭日期和打开的天数。

Here is my example and what I want to find with SQL is with the blue/grey color这是我的例子,我想用 SQL 找到的是蓝色/灰色

在此处输入图片说明

如果您已经有两个可用的日期作为OpenDateCloseDate在您的情况下,您可以使用DATEDIFF()函数,如下所示。

Select DATEDIFF(DAY, OpenDate, CloseDate) as NumberOfOpenDays from YourTable

This looks a gaps-and-islands problem -- but the islands are already defined by opendt (and perhaps col1 and col2 ).这看起来是一个间隙和岛屿问题——但这些岛屿已经由opendt (可能还有col1col2 )定义。

So, you can just use row_number() :所以,你可以只使用row_number()

select row_number() over (partition by col1, col2, opendt order by effectivedt)

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

相关问题 SQL Server从日期范围中获取天数,但不包括特定日期范围中的某些天 - SQL Server get number of days from date range excluding certain days from specific date range 日期范围内的SQL Server计数天数 - Sql server Count Days within Date Range 在没有扩展功能的SQLite中从SQL中获取日期范围内的工作日数 - get number of working days from date range in SQL for sqlite without extension functions SQL Server 2014-如何获取7天前加上1小时的日期范围 - SQL Server 2014 - How to get date range of 7 days ago plus 1 hour SQL:计算历史表上日期范围之间的天数 - SQL : calculate the number of days between date range on a history table 如何获取SQL Server中一个月的天数 - How to get number of days in a month in SQL Server 如何从日期范围中获取当前年份的天数? - How to get the number of days that are the current year from a date range? 使用SQL Server将x天数添加到sql中的日期,但还传递一周中的工作天数 - With SQL Server add x number of days to a date in sql but also pass in the number of business days in a week SQL Server查询日期范围之间一个月的总天数 - SQL Server query for total number of days for a month between date ranges 计算SQL Server中日期和今天之间的天数? - Calculating number of days between a date and today in SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM