生成日期范围之间的日期
我需要填充一个表,该表将存储 2 个给定日期之间的日期范围: 09/01/11 - 10/10/11
因此在这种情况下,该表将从 09/01/11 开始并存储每一天,直到2011年10月10日 我想知道在 SQL Server 中是否有一种巧妙的方法可以做到这一点 - 我目前正在使用 SQL Server 2008。谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
轻松使用 SQL 2005+;如果您有数字或统计表,则更容易。我在下面伪造了它:
如果您有一个统计表,请用该表替换子查询。没有递归。
编辑:由于人们似乎对统计表有疑问,让我使用从零开始的统计表重写它。首先,这是一些用于创建和填充表的代码。
现在,您已将数字表作为数据库中的永久对象,您可以将其重新用于查询而不是子查询。该查询也已被编辑为使用从零开始的计算。
高性能,并且没有递归。
Easy on SQL 2005+; easier if you have a numbers or tally table. I faked it below:
If you have a tally table, replace the subquery with the table. No recursion.
EDIT: Since folks seem to have questions about the tally table, let me rewrite this using a zero-based tally table. First, here's some code to create and populate a table.
Now, that you have the numbers table as a permanent object in your database, you can reuse it for the query INSTEAD of the subquery. The query has also been edited to use a zero-based calculation.
Performant, and no recursion.
如果您使用的是 SQL Server 2005 或更高版本,请尝试此操作:这是
一个可以使用 CTE 完成的很酷的事情的一个很好的示例。
Try this if you are using SQL Server 2005 or newer:
A good example of cool stuff you can do with a CTE.
-- 声明
-- 查询:
-- 输出
-- Declarations
-- Query:
-- Output
这是一个不需要递归的解决方案,同时,这个表值函数可以在许多查询中重复使用,而不需要再次重复声明样板变量。对于那些不想递归的人来说,这是唯一的选择。
创建这个简单的函数:
然后选择依据:
Here is a solution that does not require recursion, and at the same time, this table-valued function is re-usable in many queries without the need to repeat the declaration of boilerplate variables again. This is the only alternative, for those who don't want recursion.
Create this simple function:
And then select by:
我意识到这是一个旧线程,但我不得不承认我对这里给出的过多的递归和循环解决方案感到沮丧。我想知道有多少人意识到递归只不过是一个非常昂贵的循环?我理解创建表值函数的愿望,但我建议以下方法更加有效,因为它是基于集合的,无需循环、递归或重复的单个插入语句:
I realize that this is an old thread, but I have to admit my dismay at the overabundance of recursive and looping solutions given here. I wonder just how many folks realize that recursion is nothing more than a very expensive loop? I understand the desire to create a Table-Valued Function, but I suggest that the following is far more efficient as it is set-based, without looping, recursion, or repeated single insert statements:
这是一个旧线程,但如果它对任何人有帮助,这就是我在支持 CTE 的 SQL Server 现代版本中使用的内容。这还为您提供了星期几,并且可以对其进行调整以提供您可能需要的其他值(即季度、月份等)。
This is an old thread, but in case it helps anyone, this is what I use in modern versions of SQL Server that support CTE's. This also gives you the Day of the Week and it can be tweaked to give other values you may need (i.e. Quarter, Month, etc.).
使用MVJ的F_TABLE_DATE函数,简直太棒了:
http://www.sqlteam.com /forums/topic.asp?TOPIC_ID=61519
一旦您实现此功能,只需传入开始和结束日期,您就可以插入其间的所有日期。
Use MVJ's F_TABLE_DATE function, it is purely awesome:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519
Once you implement this just pass in start and end date and you can insert all dates between.
如果由于某种原因您无法
声明
变量,例如使用Looker 中的 ="nofollow noreferrer">派生表,你可以这样:顺便说一下,这个你的约会方式是这样的系列视图在 LookerML 中可能类似于:
If for some reason you can't
declare
variables, such as when using derived tables in Looker, you can go like this:By the way, this is how your date series view could look like in LookerML:
尝试以下代码:
检查性能,我发现使用CTE方法具有更好的性能,如图所示。为此,我使用了两个查询并使用 SQL Server 工具显示了性能。
Try Following CODE:
Examining the performance, I found that using the CTE method has a better performance that I have shown in the figure. For this purpose, I used two queries and displayed the performance using the SQL Server tool.
使用 @Abe Miesler 的答案,为了其他人的方便,我将其构建到 SQL Server 2008 及以后的 TVF 中。它可能对其他人有帮助 - 我必须找到一种方法将 CTE 包含在 TVF 中!
Using @Abe Miesler's answer, for other's convenience I built it into a TVF for SQL Server 2008 onwards. It may help others - I had to find a way to include the CTE inside the TVF!
当我们无法在数据库中创建函数时,递归查询是一个很好的选择。
MySQL 8+ & MariaDB 10.2.2+
Postgres 11+
要在 SELECT 语句中连接这些日期,您可以使用
JOIN days ON true
来复制日期范围内每个日期的行。Recursive query is a good alternative when we cannot create functions in the database.
MySQL 8+ & MariaDB 10.2.2+
Postgres 11+
To join these dates in your SELECT statement, you can use a
JOIN dates ON true
to replicate your rows for each date in your date range.我在 Databricks 上使用 ANSI 标准 SQL 方言,下面的代码是创建月份行的更简单方法:
I use the ANSI standard SQL dialect on Databricks and the code below it`s a simpler way of creating month rows:
如果您的数据库支持递归公用表表达式,那么这很容易做到。
这是 MySQL 8 上的示例。
例如,如果您希望日期向后计数,则可以使用 DATE_SUB() 并相应地调整 where 子句。您还可以根据需要将日期增加一天以上,只需以一天以上的间隔进行日期计算即可。
参考:
https://nucco.org/2023/ 02/生成a-series-of-dates-in-sql.html
This is pretty easy to do if your database supports recursive common table expressions.
Here is an example on MySQL 8.
You can use DATE_SUB() if you want the dates to count backwards, for example and adjust the where clause accordingly. You can also increment the dates by more than one day as desired by simply doing the date calculation with more than one day interval.
Reference:
https://nucco.org/2023/02/generate-a-series-of-dates-in-sql.html