简体   繁体   English

年/月/周至今 SQL 查询

[英]Year/Month/Week to date SQL query

I am currently developing a platform where I need 3 SQL queries to return all records between Year/Month/Week to date.我目前正在开发一个平台,我需要 3 个 SQL 查询来返回迄今为止年/月/周之间的所有记录。

The queries have to work dynamically without hard-coded dates, please bear with me if i should split these up into 3 questions, i think they are very relatable and that's why i put all 3 in here :)查询必须在没有硬编码日期的情况下动态工作,如果我应该将它们分成 3 个问题,请耐心等待,我认为它们非常相关,这就是我将所有 3 个问题都放在这里的原因:)

1: Year To Date 1:年初至今

I tried this SQL query according to this question about Year To Date but I am receiving an error:我根据这个关于Year To Date的问题尝试了这个SQL查询,但我收到一个错误:

Incorrect parameter count in the call to native function 'DATEDIFF'调用本机函数“DATEDIFF”时的参数计数不正确

SELECT coins_daily_date, coins_daily_high, coins_daily_low, coins_daily_close, coins_daily_volume
FROM coins_daily
WHERE coins_daily_date BETWEEN DATEADD(yy, DATEDIFF(yy,0,GETDATE()), 0) AND GETDATE()
ORDER BY coins_daily_date ASC

2: Month To Date 2:本月至今

I tried different things with the month function but could not figure out how to achieve/build a correct month to date SQL query.我用月函数尝试了不同的东西,但无法弄清楚如何实现/构建正确的月至今日 SQL 查询。

3: Week To Date 3:本周至今

Honestly, I have no idea where, to begin with, this one.老实说,我不知道从哪里开始,这个。

Works fine going back 1 year工作正常回到 1 年

SELECT coins_daily_date, coins_daily_high, coins_daily_low, coins_daily_close, coins_daily_volume
FROM coins_daily
WHERE coins_daily_date >= DATE_ADD(NOW(),INTERVAL -1 YEAR
ORDER BY coins_daily_date ASC

I hope someone can help or point me in the right direction :)我希望有人可以帮助或指出我正确的方向:)

Best regards!此致!

GETDATE() is for MsSQL. GETDATE() 用于 MsSQL。 Use NOW() for current datetime or curdate() for current date.使用 NOW() 表示当前日期时间或 curdate() 表示当前日期。

For year-to-date :年初至今:

SELECT coins_daily_date, coins_daily_high, coins_daily_low, coins_daily_close, 
coins_daily_volume
FROM coins_daily
WHERE YEAR(coins_daily_date) = YEAR(NOW())
AND coins_daily_date <= NOW()
ORDER BY coins_daily_date ASC

For month-to-date :对于本月至今:

SELECT coins_daily_date, coins_daily_high, coins_daily_low, coins_daily_close, 
coins_daily_volume
FROM coins_daily
WHERE YEAR(coins_daily_date) = YEAR(NOW())
AND MONTH(coins_daily_date) = MONTH(NOW())
AND coins_daily_date <= NOW()
ORDER BY coins_daily_date ASC

For week-to-date :对于本周至今:

SELECT coins_daily_date, coins_daily_high, coins_daily_low, coins_daily_close, 
coins_daily_volume
FROM coins_daily
WHERE YEAR(coins_daily_date) = YEAR(NOW())
AND WEEK(coins_daily_date) = WEEK(NOW())
AND coins_daily_date <= NOW()
ORDER BY coins_daily_date ASC

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

相关问题 获取星期几,星期几,月份和年份的日期 - Get Date with Day of Week, Week Number, Month and Year sql查询排序年/月 - sql query sorting year/month 如何查询上一年的一周的第一天 - How to query for the first date of a week on the PREVIOUS year MySQL:根据特定月/年的第 n 天获取日期 - MySQL: Get date based on the nth day of the week on a particular month/year 如何在单个查询中基于日期范围(最后一天,上周,上个月,去年)从表中获取数据? - how to fetch data from table based on date range(last day, last week, last month,last year) in a single query? 在MySQL中通过单一查询获取今天,本周,本月和今年的数据? - Get today,this week,this month and this year data in single query in MySQL? 在 MariaDb 查询中,给定年份、月份和星期,如何获取一周中的第一天的星期几? - How to get the day of month, of the first day of a week, given the year the month and the week in a MariaDb query? 使用 SQL 获取当前周、月和年的记录 - Get current week, month and year records using SQL SQL 中是否有更好的方法来按日/周/月/年/所有时间对操作进行分组? - Is there a better approach in SQL to group actions by day/week/month/year/alltime? SQL 按月和年选择日期范围 - SQL select date range by month and year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM