I have a set of data that provides durations of overtime taken by a user by date.
I need to further split this up in this way:
- Overtime up to and including 5 hours for the entire date range in 1 column
- Overtime after the initial 5 hours for the entire date range in another column
Because the data needs to be displayed by day, I'm having a hard time achieving this.
The data I'm working with looks like this:
SELECT
USERNAME,
DATE,
SUM (DATEDIFF(SECOND, STARTTIME, ENDTIME) / 3600.0) AS OT_DURATION
FROM EVENTS
WHERE EVENT_TYPE = 'OVERTIME'
AND DATE BETWEEN '2021-01-25' AND '2021-01-31'
;
Current output:
| USERNAME | DATE | OT_DURATION |
|----------|------------|-------------|
| bob | 2021-01-25 | 2.0 |
| bob | 2021-01-26 | 2.0 |
| bob | 2021-01-27 | 3.0 |
| bob | 2021-01-28 | 2.0 |
| bob | 2021-01-29 | 0.0 |
Desired output:
| USERNAME | DATE | OT_FIRST-5_HRS | OT_AFTER_5_HRS |
|----------|------------|---------------------------------|
| bob | 2021-01-25 | 2.0 | 0.0 |
| bob | 2021-01-26 | 2.0 | 0.0 |
| bob | 2021-01-27 | 1.0 | 2.0 |
| bob | 2021-01-28 | 0.0 | 2.0 |
| bob | 2021-01-29 | 0.0 | 0.0 |
TO clarify, OT_FIRST_5_HRS is the first 5 hrs for the entire query period, not for just the day. The same applies to OT_AFTER_5_HRS.
The 1 week period is just an example, but ideally the solution would apply to any range of dates.
Note that I'm working with SQL Server 2016.
Any help would be greatly appreciated.
question from:
https://stackoverflow.com/questions/66046355/group-durations-in-buckets-for-entire-query-date-range-then-display-by-date 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…