SQL Server - 给定周数,获取一周中的第一个日期?
我有一个查询(用于 bug tracker.net),可以按状态计算每周的 bug 数量。但查询返回周数,我真正想要的是一周的第一个日期
select datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date)))
as [week], bg_status , st_name as [status], count(*) as [count]
from bugs inner join statuses on bg_status = st_id
group by datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))),
bg_status, st_name
order by [week], bg_status
获取周数的部分是
datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week]
它返回此输出:
week bg_status status count
----------- ----------- --------------------------------------------- ------
22 1 new 1
22 5 closed 32
但最好说每周的第一个日期,例如01-01 -2010,然后 08-01-2010 等
问题不是 如何从 SQL Server 中的周数获取“周开始日期”和“周结束日期”?(答案说明如何从日期而不是周数开始周)
不是 从周数计算日期的重复项 (问题要求使用 c#)
不与 获取第一个重复提供的日期中的星期几(问题要求使用javascript)
我进行了搜索,但找不到针对 SQL Server 的此问题答案(2010 年,如果重要的话)
I've got a query (for use in bug tracker.net) that calculates the number of bugs by week by status. But the query returns the week number, what I really want is the first date of the week
select datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date)))
as [week], bg_status , st_name as [status], count(*) as [count]
from bugs inner join statuses on bg_status = st_id
group by datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))),
bg_status, st_name
order by [week], bg_status
The part that gets the week number is
datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week]
It returns this output:
week bg_status status count
----------- ----------- --------------------------------------------- ------
22 1 new 1
22 5 closed 32
But it would be better to say the first date of each week, eg 01-01-2010, then 08-01-2010, etc
Question is not a duplicate of How do you get the "week start date" and "week end date" from week number in SQL Server? (answer says how to get week start from a date not from a week number)
Not a duplicate of Calculate date from week number (question asks for c#)
Not a duplicate of Get first date of week from provided date (question asks for javascript)
I did search but couldn't find this question answered for SQL Server (2010 if it matters)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你以正确的方式思考它,答案 SO 1267126 可以应用于您的问题。
组中的每个错误报告日期都映射到同一周。因此,根据定义,每个错误日期也必须映射到同一周的开始。因此,您对错误报告日期运行“从给定日期开始的一周”计算以及周数计算,并按两个(适度可怕的)表达式进行分组,最后得到您寻求的答案。
由于
bg_reported_date
是一个 DATETIME (请参阅注释;它包含时间部分),因此有必要在确定周开始之前将其转换为 DATE (但周数表达式不需要转换) ,并且一周开始表达式的“星期几”部分也不需要强制转换):注意:未经测试的代码!
If you think about it in the right way, the answer to SO 1267126 can be applied to your problem.
Each of the bug reported dates that you have in the group maps to the same week. By definition, therefore, each of those bug dates must also map to the same start of the week. So, you run the 'start of the week from given date' calculation on the bug report dates, as well as the week number calculation, and group by both (modestly ghastly) expressions, and end up with the answer you seek.
Since
bg_reported_date
is a DATETIME (see the comment; it includes a time component), it is necessary to cast it to DATE before determining the week-start (but the week number expression doesn't need the cast, and the 'day of week' part of the week start expression doesn't need the cast either):NB: Untested code!
我意识到这是一个非常古老的线程,但是“在给定周数的情况下获取一周内的第一次约会”正是我想要做的,而且我没有实际的日期可以使用,所以接受的答案不适用于我。我想我应该为后代发布我的解决方案。请注意,我怀疑不同的文化设置可能会破坏这一点,因此在使用之前进行测试。
我的答案是从这个开始构建的。
假设您知道周数和年份,并且您想要获取该年该周的开始日期和结束日期。这是我所拥有的:
I realize this is a very old thread, but "Get first date in a week, given the week number" is exactly what I wanted to do and I do NOT have an actual date to work with, so the accepted answer would not work for me. I thought I'd post my solution for posterity. Note that I suspect different culture settings MAY break this, so test before using.
My answer is built starting from this one.
Let's assume you know a week number and a year and you want to get the start and end dates for that week of that year. Here's what I have: