将内容放入 SQL Server 2005 中的日期范围
我有一个包含周范围(周数、开始日期、结束日期)的表和一个包含教程日期的表(用于编写导师(导师 ID、教程日期、教程类型(A 或 B))。
我想创建两个查询来显示顶部显示周范围(第 1 周、第 2 周),侧面显示导师姓名,并在该周的每个块的日期范围内显示教程数量(类型“A”)。
结果应如下所示:
计数。 “A”类型教程的
Tutor|Week One|Week Two|Week Three|Week Four|Total
Joe | 3 | 5 | 7 | 8 | 23
Sam | 2 | 4 | 3 | 8 | 17
数量意味着 Joe 在第一周完成了 3 个教程,在第二周完成了 5 个教程,在第三周完成了 7 个教程,在第 4 周完成了 8 个教程。
第二个查询应显示教程类型“A”和类型“B”的总计
Tutor|Week One|Week Two|Week Three|Week Four|Total |
Joe | 3/1 | 5/3 | 7/2 | 8/2 | 23/8 |
Sam | 2/3 | 4/4 | 3/2 | 8/3 | 17/12 |
在第一周,Joe 完成了 3 个 A 类教程和 1 个 B 类教程。
教程的示例表数据(第一周)
Tutor | Tutorial_ID | Tutorial Date |Type|
------------------------------------------
Joe | 1 | 2011-01-01 | A |
Joe | 2 | 2011-01-02 | A |
Joe | 3 | 2011-01-03 | A |
Joe | 4 | 2011-01-03 | B |
Sam | 5 | 2011-01-01 | A |
Sam | 6 | 2011-01-02 | A |
Sam | 7 | 2011-01-03 | B |
周表如下所示:
weekNumber |startDate |endDate
1 |2011-01-01|2011-01-15
I'd like to gen this in SQL Server 2005
I have a table with week ranges (week number,start date, end date) and a table with tutorial dates (for writing tutors (tutor ID, tutorial_date, tutorial type(A or B).
I want to create two query that shows the week ranges (week 1, week 2) across the top with the tutor names on the side with count of tutorials (of type "A") in that week's date range in each block for that week.
The result should look like this:
Counts of Tutorials of Type "A"
Tutor|Week One|Week Two|Week Three|Week Four|Total
Joe | 3 | 5 | 7 | 8 | 23
Sam | 2 | 4 | 3 | 8 | 17
Meaning that Joe completed 3 tutorials in week one, five in week two, 7 in week three, and 8 in week 4.
The second query should show totals for tutorial type "A" and type "B"
Tutor|Week One|Week Two|Week Three|Week Four|Total |
Joe | 3/1 | 5/3 | 7/2 | 8/2 | 23/8 |
Sam | 2/3 | 4/4 | 3/2 | 8/3 | 17/12 |
Here, in Week One, Joe has done 3 tutorials of type A and 1 of type B.
Sample table data for tutorials (week one)
Tutor | Tutorial_ID | Tutorial Date |Type|
------------------------------------------
Joe | 1 | 2011-01-01 | A |
Joe | 2 | 2011-01-02 | A |
Joe | 3 | 2011-01-03 | A |
Joe | 4 | 2011-01-03 | B |
Sam | 5 | 2011-01-01 | A |
Sam | 6 | 2011-01-02 | A |
Sam | 7 | 2011-01-03 | B |
The week table looks like this:
weekNumber |startDate |endDate
1 |2011-01-01|2011-01-15
I'd like to gen this in SQL Server 2005
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以做到这一点。
对于查询一,您只需在类型“A”上
PIVOT
,然后您只需执行PIVOT
,请参阅SQL Fiddle 与演示
或者您可以将
Count()
与CASE 一起使用声明。
请参阅 SQL Fiddle with Demo
但对于第二个查询,我只需使用
Count()
和CASE
请参阅 SQL Fiddle with Demo
编辑为 AndriyM 指出第二个可以通过 PIVOT 完成,这里是第二个查询的解决方案:
请参阅 SQL 摆弄演示
There are a few ways to do this.
For query one, where you only need to
PIVOT
on type 'A' then you can do just aPIVOT
See SQL Fiddle with Demo
Or you can use a
Count()
with aCASE
statement.See SQL Fiddle with Demo
But for the second query, I would just use a
Count()
with aCASE
See SQL Fiddle with Demo
Edit as AndriyM pointed out the second could be done with a PIVOT here is a solution for the Second query:
See SQL Fiddle with Demo