简体   繁体   English

将具有指定日期范围的计算总和从一个表连接到另一个表

[英]Joining calculated sum with specified daterange from one table to another

I am trying to get calculated overtime in my wanted results for each user for a specific period(21st last month to 20th current month, or todays date if lower than 20)我正在尝试在特定时期(上个月 21 日至本月 20 日,或者如果低于 20 日,则为今天的日期)在我想要的结果中为每个用户计算加班时间

HOURS小时

id  | user_id |       starttime      |        endtime       |      created_at     |
1   |    1    |  2018-05-10 05:00:00 |  2018-05-10 16:00:00 | 2018-07-10 16:30:53 | 
2   |    1    |  2018-08-28 06:00:00 |  2018-08-28 14:30:00 | 2018-08-28 17:00:16 |
3   |    2    |  2018-08-28 06:00:00 |  2018-08-28 18:00:00 | 2018-08-28 17:55:22 |
3   |    3    |  2018-08-28 06:00:00 |  2018-08-28 12:00:00 | 2018-08-28 12:55:22 |
3   |    3    |  2018-08-28 06:00:00 |  2018-08-28 12:00:00 | 2018-08-28 12:56:22 |

USERS用户

id | name |      email      |   phone   |      adress      |
1  | jane | gmail@gmail.com | 12312312  | somestreet 12    |
2  | john | yahoo@yahoo.com | 44433322  | anotherstreet 23 |
3  | joe  | joe@email.com   | 12344432  | paradise 223     |

This is what i want to do - with the overtimetotal having overtime in seconds for specific period WANTED RESULT这就是我想要做的 - 加班总数在特定时期内以秒为单位加班 通缉结果

id | name |      email      |   phone   |      adress      | overtimeTotal |
1  | jane | gmail@gmail.com | 12312312  | somestreet 12    |    234000     |
2  | john | yahoo@yahoo.com | 44433322  | anotherstreet 23 |     39240     |
3  | joe  | joe@email.com   | 12344432  | paradise 223     |         0     |

I have been trying to do this for several hours now, looking at different solutions here on stack - but nothing seems to work for me.我一直在尝试这样做几个小时,在堆栈上查看不同的解决方案 - 但似乎没有什么对我有用。

Basically this is the best i have been able to produce:基本上这是我能够制作的最好的:

SELECT u.*, r.overTimeTotal from users u
INNER JOIN 
(
    SELECT user_id,
    SUM( 
        IF( TIMESTAMPDIFF(SECOND, starttime, endtime) >= TIME_TO_SEC('07:24:00:0000')
           , TIMESTAMPDIFF(SECOND, starttime, endtime)  - TIME_TO_SEC('07:24:00:0000')
           , 0
          )  
    ) as overTimeTotal FROM hours
    WHERE starttime BETWEEN 2020-06-21 AND 2020-07-20
) r ON u.id

This almost does what i want, but it adds all the overtime for the period for all users and uses it in the overtimetotal field.这几乎可以满足我的要求,但它会为所有用户添加该期间的所有加班时间,并在 overtimetotal 字段中使用它。

id | name |      email      |   phone   |      adress      | overtimeTotal |
1  | jane | gmail@gmail.com | 12312312  | somestreet 12    |    273240     |
2  | john | yahoo@yahoo.com | 44433322  | anotherstreet 23 |    273240     |
3  | joe  | joe@email.com   | 12344432  | paradise 223     |    273240     |

What am i doing wrong?我究竟做错了什么? Can't wrap my head around it.不能把我的头绕过去。 I have really tried alot of things, LATERAL LEFT JOIN and so on.我真的尝试了很多东西,LATERAL LEFT JOIN 等等。

SOLUTION解决方案

I finally managed to solve this with help from the comments我终于在评论的帮助下解决了这个问题

SELECT u.name, u.email,
u.id,
(
    SELECT 
        SUM(
            IF(
                TIMESTAMPDIFF(SECOND, h.starttime, h.endtime) >= TIME_TO_SEC('07:24:00:0000'),
                TIMESTAMPDIFF(SECOND, h.starttime, h.endtime) - TIME_TO_SEC('07:24:00:0000'),
                0
              )
           )
    FROM hours h
    WHERE DATE(h.starttime) BETWEEN '2020-06-21' AND '2020-07-20'
    AND h.user_id = u.id
) AS overtimeTotal
FROM hours h
INNER JOIN users u on h.user_id = u.id
GROUP BY u.id

seems your ON condition is not completed (you should join the user_id matching values) and you need a proper group by for get each user result似乎您的 ON 条件未完成(您应该加入 user_id 匹配值)并且您需要一个适当的 group by 来获取每个用户结果

    SELECT u.*, r.overTimeTotal 
    from users u
    INNER JOIN 
    (
        SELECT user_id,
        SUM( 
            IF( TIMESTAMPDIFF(SECOND, starttime, endtime) >= TIME_TO_SEC('07:24:00:0000')
               , TIMESTAMPDIFF(SECOND, starttime, endtime)  - TIME_TO_SEC('07:24:00:0000')
               , 0
              )  
        ) as overTimeTotal 
        FROM hours
        WHERE starttime BETWEEN 2020-06-21 AND 2020-07-20
        GROUP BY user_id
    ) r ON u.id = r.user_id

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

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