选择每天员工缺勤的计数

发布于 2024-11-03 01:53:07 字数 455 浏览 4 评论 0原文

好吧,假设我有三个表,如下所示:
员工(ID,姓名)
会话(ID、starts_at、ends_at、day)
缺勤(ID、employeeID、sessionID)

可以说每天有多个会话。
是否可以选择计算会话表上每个不同日期的会话中员工的缺勤情况并将其分组如下:(

--------------------------------
| Name |2011-01-15| 2011-01-16 | 
|      |          |            |
|john  |  2       |   0        |
|dave  |  4       |    1       |

列数(天数)取决于会话表上的天数,假设我只想要那些上周或本周
谢谢。

alright, so lets say i have three tables, as follows :
employees(ID, name)
sessions(ID, starts_at, ends_at, day)
absences(ID, employeeID, sessionID)

lets say there are multiple sessions per day.
is it possible to select count the Absences of employees from sessions for each distinct day on the sessions table and group them as follows :

--------------------------------
| Name |2011-01-15| 2011-01-16 | 
|      |          |            |
|john  |  2       |   0        |
|dave  |  4       |    1       |

(number of columns (days) depends on number of days on the sessions table, lets say i only want those of last or current week
thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2024-11-10 01:53:07

您应该使用 PIVOT 语句。就这样:

SELECT Name, [2011-01-15], [2011-01-16]
FROM    
(SELECT  [Day],Name
    FROM [Absences] A left join
         [Sessions] S on A.sessionID = S.ID
    GROUP BY [Day],Name) AS SourceTable
PIVOT
(
COUNT([Day])
FOR [Day] IN ([2011-01-15], [2011-01-16])
) AS PivotTable;

You should use the PIVOT statement. There it goes:

SELECT Name, [2011-01-15], [2011-01-16]
FROM    
(SELECT  [Day],Name
    FROM [Absences] A left join
         [Sessions] S on A.sessionID = S.ID
    GROUP BY [Day],Name) AS SourceTable
PIVOT
(
COUNT([Day])
FOR [Day] IN ([2011-01-15], [2011-01-16])
) AS PivotTable;
温柔少女心 2024-11-10 01:53:07

查找数据透视表。在某些 RDBMS 上,这是内置的。

Look up pivot table. On some RDBMS, this is built in.

灯角 2024-11-10 01:53:07

我认为你正在谈论 MySQL 并且你想做一个数据透视表。访问 ArtfulSoftware 的 MySQL 站点并查看数据透视表教程:
http://www.artfulsoftware.com/infotree/queries.php

I'm thinking you are talking MySQL and you want to do a pivot table. Go to ArtfulSoftware's MySQL site and review the pivot table tutorials:
http://www.artfulsoftware.com/infotree/queries.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文