简体   繁体   English

仅当 first_stamp 位于 start_dt 的窗口之间时员工计数

[英]Count of Employee only when first_stamp is between a window of start_dt

I want a count of emp_id only when first stamp date is in a 21 day window depending on startdt.仅当第一个戳记日期在 21 天的窗口中时,我才需要 emp_id 的计数,具体取决于 startdt。

However, I want to count that empid, only when the firststamp either 7 days before the startdt, during the week of startdt, or the next week of startdt.但是,我想计算那个 empid,只有当 firststamp 在 startdt 前 7 天、在 startdt 的那一周或 startdt 的下一周。 Startdt is always going to be a sunday, so essentially I am looking for a three week period, 1 week before the startdt, during the week of start dt or one week after. Startdt 总是星期天,所以基本上我正在寻找一个三周的时间段,在 startdt 之前 1 周,在 startdt 的那一周或之后的一周。

For example, in this table, employee 123 will be given a count since his first stamp is 10/21(during the week of start dt) for this emp any date between and including 10/09 and 10/29 will be acceptable.例如,在此表中,员工 123 将被计数,因为他的第一个戳记是 10/21(在开始 dt 的那一周),对于此 emp,任何介于 10/09 和 10/29 之间的日期都是可以接受的。

But emp 345 does not get a credit because his first stamp is way after the 21 day window.但是 emp 345 没有得到积分,因为他的第一张邮票已经过了 21 天的时间。 It would be acceptable if it was between 10/16 to 11/04如果在 10/16 到 11/04 之间是可以接受的

Can someone help me figure out the code logic in SQL?有人可以帮我弄清楚 SQL 中的代码逻辑吗? enter image description here在此处输入图像描述

please let me know if you have any questions请让我知道,如果你有任何问题

I have a MySQL answer for you.我有一个 MySQL 答案给你。

If you are using MySQL ( I am saying this because I dont know what 'DATE' data requirements and the clause that we can use to query them in other RDBMS.),first of all, the data that you stored in the 'DATE' column should not be arbitrary, it should be something like YYYY-MM-DD.如果您使用的是 MySQL(我这么说是因为我不知道什么是“DATE”数据要求以及我们可以用来在其他 RDBMS 中查询它们的子句。),首先,您存储在“DATE”中的数据列不应该是任意的,它应该是像 YYYY-MM-DD 这样的东西。 Once you insert your date data as 'YYYY-MM-DD', MySQL provides some clause that we can use.一旦您将日期数据插入为“YYYY-MM-DD”,MySQL 会提供一些我们可以使用的子句。

The following is the code that you need:以下是您需要的代码:

SELECT COUNT(emp_id) AS emp_id_count
FROM t1
WHERE first_stamp BETWEEN DATE_SUB(start_dt, INTERVAL 7 DAY) AND DATE_ADD(start_dt, INTERVAL 13 DAY);

I have tested it, it works.我已经测试过了,它有效。 The only prerequest is that you have to change your date data to YYYY-MM-DD.唯一的先决条件是您必须将日期数据更改为 YYYY-MM-DD。 And also, you can change t1 to your actual table name.而且,您可以将 t1 更改为您的实际表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM