简体   繁体   English

计算日期范围内的天数?

[英]Count days in date range?

I have a query like this:我有一个这样的查询:

SELECT COUNT(*) AS amount
FROM daily_individual_tracking
WHERE sales = 'YES'
AND daily_individual_tracking_date BETWEEN '2010-01-01' AND '2010-03-31'

I am selected from a date range.我是从一个日期范围中挑选出来的。 Is there a way to also get the total days in the date range?有没有办法获得日期范围内的总天数?

Not really clear if you are looking for不是很清楚你是否正在寻找

DATEDIFF('2010-03-31', '2010-01-01')

or或者

COUNT(DISTINCT daily_individual_racking_date)

You can use the MySQL datediff function:您可以使用 MySQL datediff函数:

SELECT DATEDIFF('2010-01-01','2010-01-31') AS DiffDays

It should return a floating point, where 1.0 represents a single day.它应该返回一个浮点数,其中1.0代表一天。

And for MS SQL use ,对于 MS SQL 使用,

SELECT DATEDIFF( day ,'2010-01-01','2010-01-31') AS DiffDays

What exactly are you trying to count?你到底想数什么? The total number of distinct values of daily_individual_tracking_date? Daily_individual_tracking_date 的不同值的总数? Do you need it in the same query as the count(*) query?您是否需要在与 count(*) 查询相同的查询中使用它?

This depends on what SQL server you're using.这取决于您使用的 SQL 服务器。

If you're using MS-SQL Server, you can use the function DateDiff如果您使用的是 MS-SQL Server,则可以使用函数DateDiff

I'm not sure which SQL you are using.我不确定您使用的是哪种 SQL。 TSQL has a DATEDIFF that will count the number of days between two dates. TSQL 有一个 DATEDIFF 将计算两个日期之间的天数。 See this看到这个

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

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