将行更改为列并计数
如何根据行计算计数?
源表 每个员工可以休假 2 天
Employee-----First_Day_Off-----Second_Day_Off
1------------10/21/2009--------12/6/2009
2------------09/3/2009--------12/6/2009
3------------09/3/2009--------NULL
4
5
.
.
.
现在我需要一个表格来显示当天休假的日期和人数
Date---------First_Day_Off-------Second_Day_Off
10/21/2009---1-------------------0
12/06/2009---1--------------------1
09/3/2009----2--------------------0
有什么想法吗?
how to calculate count based on rows?
SOURCE TABLE
each employee can take 2 days off
Employee-----First_Day_Off-----Second_Day_Off
1------------10/21/2009--------12/6/2009
2------------09/3/2009--------12/6/2009
3------------09/3/2009--------NULL
4
5
.
.
.
Now i need a table that shows the dates and number of people taking off on that day
Date---------First_Day_Off-------Second_Day_Off
10/21/2009---1-------------------0
12/06/2009---1--------------------1
09/3/2009----2--------------------0
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Oracle 9i+,使用子查询分解 (WITH):
非子查询版本
Oracle 9i+, using Subquery Factoring (WITH):
Non Subquery Version
处理这些查询有点尴尬,因为您的休息日存储在不同的列中。更好的布局是这样的
然后,如果一名员工请了多天假,您将有多行
在这种情况下,您可以使用以下查询找出每个人请了多少天假:
以及请了多少天假每个日期的休息日如下:
但我离题了...
您可以尝试使用 SQL CASE 语句来帮助解决这个问题:(
请注意,您可能需要根据您的数据库稍微更改语法。
获取当天起飞的日期和人数可能会更复杂。
我不知道这是否会工作,但你可以尝试一下:
It is a bit awkward to handle these queries, since you have days off stored in different columns. A better layout would be to have something like
Then you would have multiple rows if an employee took multiple days off
In that case, you could find out how many days off each person took by using the following query:
And the number of people who took days off on each date like this:
But I digress...
You can try to use an SQL CASE statement to help with this:
(note that you may need to change around the syntax slightly depending on your database.
Getting dates and number of people who took off on that day might be more complicated.
I don't know if this would work, but you can try it: