SQL查询每日出勤记录

发布于 2025-01-25 22:22:06 字数 1290 浏览 1 评论 0原文

您可以帮助我使用查询转换这些数据吗?

IDDateTime状态
12022-04-01 07:38:31时间在
12022-04-01 12:07:17中断
12022-04-01 12:07:39突破
12022-04-01 16: 43:17超时
12022-04-02 07:38:31时间在
12022-04-02 12:07:39突破
ID ID日期日期时间突破超时时间
12022-04-0107:38 :3112:07:1712:07:3916:43:17
12022-04-0207:38:3112:07:39
12022-04-04-03
12022-04-04-04
12022-04--04--04-- 05
12022-04-06

表日期应为2022-04-30。

Can you help me transform this data using query?

iddatetimestatus
12022-04-01 07:38:31Time In
12022-04-01 12:07:17Break In
12022-04-01 12:07:39Break Out
12022-04-01 16:43:17Time Out
12022-04-02 07:38:31Time In
12022-04-02 12:07:39Break Out
iddateTime InBreak InBreak OutTime Out
12022-04-0107:38:3112:07:1712:07:3916:43:17
12022-04-0207:38:3112:07:39
12022-04-03
12022-04-04
12022-04-05
12022-04-06

The table date should be up to 2022-04-30.

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

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

发布评论

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

评论(1

苦行僧 2025-02-01 22:22:06

您需要使您使用的SQL类型使日期函数正确,这是T -SQL(MS SQL Server)代码:

SELECT ID, 
       convert(date,Datetime,111) as Day,
       MAX(CASE WHEN status = 'Time In' THEN Datetime ELSE NULL END) as TimeIn,
       MAX(CASE WHEN status = 'Break In' THEN Datetime ELSE NULL END) as BreakIn,
       MAX(CASE WHEN status = 'Break Out' THEN Datetime ELSE NULL END) as BreakOut,
       MAX(CASE WHEN status = 'Time Out' THEN Datetime ELSE NULL END) as TimeOut
 FROM mytest
GROUP BY ID, 
         convert(date,Datetime,111)

A建议:考虑更改DateTime列的名称作为列的名称会在某个时候回来困扰您。

另外,如果您只需要时间部分,则将任何适合您的SQL风味的逻辑应用于DateTime中的任何逻辑。

You'll need to make the date function correct for the type of SQL you're using, this is T-SQL (MS SQL Server) code:

SELECT ID, 
       convert(date,Datetime,111) as Day,
       MAX(CASE WHEN status = 'Time In' THEN Datetime ELSE NULL END) as TimeIn,
       MAX(CASE WHEN status = 'Break In' THEN Datetime ELSE NULL END) as BreakIn,
       MAX(CASE WHEN status = 'Break Out' THEN Datetime ELSE NULL END) as BreakOut,
       MAX(CASE WHEN status = 'Time Out' THEN Datetime ELSE NULL END) as TimeOut
 FROM mytest
GROUP BY ID, 
         convert(date,Datetime,111)

A suggestion: consider changing the name of the datetime column - giving a column a reserved word as a column name will come back to haunt you at some point.

Also, if you want just the time portion, then apply whatever logic is appropriate to your SQL flavor to parse that from the Datetime.

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