帮助将本周查询从 Oracle PL/SQL 转换为 SQL Server 2008
我在 Oracle 数据库中运行了以下查询,并且希望获得 SQL Server 2008 数据库的等效查询:
SELECT TRUNC( /* Midnight Sunday */
NEXT_DAY(SYSDATE, 'SUN') - (7*LEVEL)
) AS week_start,
TRUNC( /* 23:59:59 Saturday */
NEXT_DAY(NEXT_DAY(SYSDATE, 'SUN') - (7*LEVEL), 'SAT') + 1
) - (1/(60*24)) + (59/(60*60*24)) AS week_end
FROM DUAL
CONNECT BY LEVEL <= 4 /* Get the past 4 weeks */
该查询的作用是获取过去 4 周的一周开始时间和一周结束时间。周数是任意的,并且应该可以在我想要的 SQL Server 查询中轻松修改。它生成如下数据:
WEEK_START WEEK_END
2010-03-07 00:00:00 2010-03-13 23:59:59
2010-02-28 00:00:00 2010-03-06 23:59:59
...
我目前正在翻译的部分是 CONNECT BY LEVEL,因为 SQL Server 2008 似乎没有等效的部分。我更愿意简单地调整像 CONNECT BY LEVEL <= 4 这样的行,并让查询生成更多或更少的周(即,我不想调整多个 UNION ALL 语句)。
编辑:到目前为止,这是我获取本周开始和结束的内容:
SELECT week_start,
DATEADD(SECOND, -1, DATEADD(DAY, 7, week_start)) AS week_end
FROM (
SELECT CAST(
CONVERT(
VARCHAR(10),
DATEADD(DAY, 1-DATEPART(DW, GETDATE()), GETDATE()),
111
) AS DATETIME
) AS week_start
) AS week_start_view
我不介意查询是否显示当前周的开始和结束日期,或者是否从上周开始。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我修改了 OMG Ponies 的回答,因为它看起来是个好主意,只是每周的
week_end
值错误,并且还显示未来几周而不是过去几周。我想出了以下内容:它生成过去 4 周:
这比我的 上一个答案,我认为,因为它不依赖于另一个具有特定行数的表。只需更改一位数字即可更改生成的周数:
SELECT DATEADD(WEEK, -3, GETDATE()) ASstarting_date
中的 3。包括当前周,该数字表示应显示当前周之前的其他周数。迭代过去几周至今,不包括当前周
更新:这是排除当前周的版本:
其结果:
迭代过去几个月至今
我后来发现需要这个查询的每月版本。这是修改:
I modified OMG Ponies' answer because it looked like a good idea, it just had the wrong
week_end
value on each week and also showed future weeks instead of past weeks. I came up with the following:It generates the past 4 weeks:
This is better than my previous answer, I think, because it does not rely on another table having a particular number of rows. The number of weeks generated can be changed by altering only one digit: the 3 in
SELECT DATEADD(WEEK, -3, GETDATE()) AS starting_date
. The current week is included, and that digit represents how many additional weeks prior to the current week should be shown.Iterate over Past Weeks up to Now, Excluding Current Week
Update: and here's a version that excludes the current week:
Its results:
Iterate over Past Months up to Now
I later found a need for a monthly version of this query. Here is that modification:
使用(但不要忘记 投票给 Sarah):
以前:
使用 SET DATEFIRST 命令:
<前><代码>设置日期优先 7
SQL Server 2005+ 相当于 Oracle 的
CONNECT BY LEVEL
是递归 CTE(ANSI 标准顺便说一句)。使用:参见此链接了解如何获取一周的第一天。要更改周数,请更改
DATEADD(dd, 4*7, GETDATE())
,其中4
表示您想要生成的周数。Use (but don't forget to vote for Sarah):
Previously:
Set the first day of the week to be Sunday with the SET DATEFIRST command:
The SQL Server 2005+ equivalent to Oracle's
CONNECT BY LEVEL
is the recursive CTE (ANSI standard btw). Use:See this link for explaining how to get the first day of the week. To alter the number of weeks, change the
DATEADD(dd, 4*7, GETDATE())
, where4
represents the number of weeks you want generated.您所需要的只是一个集合:
但是我会提醒您,如果您的数据是日期时间并且您打算将这些边界用于查询范围,则应该使用开放式范围,例如 >= 03/07 和 <= 03/07 和 < 03/14。这样您就不会错过 23:59:59 到午夜之间发生的任何行;尽管它们可能很罕见,但它们可能很重要。
All you need is a set:
However I will caution you that if your data is datetime and you are going to use these boundaries for query ranges, you should use an open-ended range, e.g. >= 03/07 and < 03/14. This way you don't miss out any rows that happened between 23:59:59 and midnight; as rare as they may be, they could be important.
根据您的代码:
based on your code:
这就是我的想法。它有点笨拙,因为它至少依赖于我的一个表,其行数>=我想要选择的周数。我可以稍微调整一下以包括本周。
这获取从上周开始的过去 21 周。这是示例数据:
Here's what I came up with. It's slightly janky in that it relies on at least one of my tables having rows >= the number of weeks I want to select. I could adjust it slightly to include the current week.
This gets the past 21 weeks, starting at last week. Here's sample data: