SQL Server How to calculate another GROUP BY with SUM in an existing query

nwnhqdif  于 2023-08-02  发布在  其他
关注(0)|答案(2)|浏览(123)

I have the following code that shows me the below results. I need to take these results and then SUM the number in the Count column and then GROUP BY the Week Of so that I have a sum of intervals per Week Of . Is this done via a sub query? I am not sure how to accomplish this.

DECLARE @interval as INT = 30;
DECLARE @StartDate as datetime = '2023-01-01 00:00:00';

SELECT
    owneridname [Owner],
    CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date) [Week Of],
        /* Calculates the amount of minutes from the start date and the Created On date using the DATEDIFF function, then divide by 30 to get the total number of 30min increments in that time frame. */
        /* This could end up as a decimal so we use FLOOR() to get the closest integer value. */
        /* We then multiply by 30 to bring it back to the Created On's date but at the appropriate interval */
    CAST(DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE, @StartDate, createdon) / CAST(@interval as decimal)) * @interval, @StartDate) as TIME(0)) [createdon_datetime_30_min_interval],
        /* Counting the number of case creation dates per interval */
    COUNT(*) [Count]
FROM incident
WHERE 
    createdon > DATEADD(day, -5, GETDATE()) 
GROUP BY
        /* Grouping by the 30min intervals */
    CAST(DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE, @StartDate, createdon) / CAST(@interval as decimal)) * @interval, @StartDate) as TIME(0)),
    owneridname,
    CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date)
ORDER BY [Owner] ASC;
OwnerWeek OfIntervalCount
John Smith2023-07-2421:00:0012
Brad Pitt2023-07-2400:30:001
Brad Pitt2023-07-2400:00:001
Neil Young2023-07-3116:30:001
Neil Young2023-07-2411:00:0010
Neil Young2023-07-2415:30:005
Neil Young2023-07-2412:30:001
Neil Young2023-07-2411:00:001
Neil Young2023-07-2411:30:001
Mark Johnson2023-07-3122:00:001
Mark Johnson2023-07-3122:30:001
Mark Johnson2023-07-2417:30:001
Mark Johnson2023-07-2421:30:003
Mark Johnson2023-07-3123:00:001

Next set of results I am looking for that show the sum of intervals per the `Week Of:
| Owner | Week Of | Sum |
| ------------ | ------------ | ------------ |
| John Smith | 7/24/2023 | 12 |
| Brad Pitt | 7/24/2023 | 2 |
| Neil Young | 7/24/23 | 18 |
| Neil Young | 7/31/2023 | 1 |
| Mark Johnson | 7/24/2023 | 4 |
| Mark Johnson | 7/31/2023 | 3 |

cygmwpex

cygmwpex1#

You can just wrap your query with outer query.

DECLARE @interval as INT = 30;
DECLARE @StartDate as datetime = '2023-01-01 00:00:00';

SELECT [Owner], [Week Of], SUM([Count])
FROM (
    SELECT
        owneridname [Owner],
        CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date) [Week Of],
            /* Calculates the amount of minutes from the start date and the Created On date using the DATEDIFF function, then divide by 30 to get the total number of 30min increments in that time frame. */
            /* This could end up as a decimal so we use FLOOR() to get the closest integer value. */
            /* We then multiply by 30 to bring it back to the Created On's date but at the appropriate interval */
        CAST(DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE, @StartDate, createdon) / CAST(@interval as decimal)) * @interval, @StartDate) as TIME(0)) [createdon_datetime_30_min_interval],
            /* Counting the number of case creation dates per interval */
        COUNT(*) [Count]
    FROM incident
    WHERE 
        createdon > DATEADD(day, -5, GETDATE()) 
    GROUP BY
            /* Grouping by the 30min intervals */
        CAST(DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE, @StartDate, createdon) / CAST(@interval as decimal)) * @interval, @StartDate) as TIME(0)),
        owneridname,
        CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date)
) d
GROUP BY [Owner], [Week Of]
ORDER BY [Owner] ASC;
vwoqyblh

vwoqyblh2#

DECLARE @interval as INT = 30;
DECLARE @StartDate as datetime = '2023-01-01 00:00:00';
    
WITH interval_count AS (
    SELECT
        owneridname [Owner],
        CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date) [Week Of],
        COUNT(*) [Count]
    FROM incident
    WHERE 
        createdon > DATEADD(day, -5, GETDATE())
    GROUP BY
        CAST(DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE, @StartDate, createdon) / CAST(@interval as decimal)) * @interval, @StartDate) as TIME(0)),
        owneridname,
        CAST(DATEADD(week, DATEDIFF(week, 0, createdon), 0) as date)
)
SELECT 
    Owner,
    [Week Of],
    SUM(Count) AS Sum
FROM interval_count
GROUP BY Owner, [Week Of]
ORDER BY Owner ASC, [Week Of] ASC;

相关问题