Salve,
non ho ben compreso l'utilita' del raggruppamento
...
group by data,eventType, ore, minuti
personalmente, va bene filtrando per data (anno e mese) e per [username], sempre che abbia ben compreso la richiesta, opterei come segue, brutalmente
SET NOCOUNT ON;
DECLARE @mainGrid table (
username varchar(20),
eventType varchar(20),
data date,
ore int,
minuti int
);
INSERT INTO @mainGrid
VALUES ( 'mario_bianchi', 'Change', '2020-01-01', 0, 30 ),
( 'mario_bianchi', 'Change', '2020-01-02', 1, 30 ),
( 'mario_bianchi', 'Change', '2020-01-03', 2, 30 ),
( 'mario_bianchi', 'Change', '2020-01-04', 3, 30 ),
( 'mario_bianchi', 'Service request', '2020-01-01', 1, 30 ),
( 'mario_bianchi', 'Service request', '2020-01-03', 0, 30 ),
( 'mario_bianchi', 'Service request', '2020-01-04', 4, 30 ),
( 'mario_bianchi', 'Service request', '2020-01-05', 5, 30 ),
( 'mario_bianchi', 'Service request', '2020-01-06', 6, 30 ),
( 'mario_bianchi', 'Servizio', '2020-01-01', 0, 30 ),
( 'mario_bianchi', 'Servizio', '2020-01-02', 1, 30 ),
( 'mario_bianchi', 'Servizio', '2020-01-05', 2, 30 ),
( 'mario_bianchi', 'Servizio', '2020-01-06', 4, 30 );
DECLARE @userName varchar(20) = 'mario_bianchi';
DECLARE @month int = 1;
DECLARE @year int = 2020;
SELECT g.[data] --, g.[username]
, SUM(
CASE WHEN g.[eventType] = 'Change' THEN
g.[ore] * 60.0 + g.[minuti]
ELSE 0.0
END
) AS [Change]
, SUM(
CASE WHEN g.[eventType] = 'Service request' THEN
g.[ore] * 60.0 + g.[minuti]
ELSE 0.0
END
) AS [Service request]
, SUM(
CASE WHEN g.[eventType] = 'Servizio' THEN
g.[ore] * 60.0 + g.[minuti]
ELSE 0.0
END
) AS [Servizio]
FROM @mainGrid g
WHERE DATEPART(YY, g.[data]) = @year
AND DATEPART(M, g.[data]) = @month
AND g.[username] = @userName
AND g.[eventType] IN ('Change', 'Service request', 'Servizio')
GROUP BY g.[data]
--, g.[username]
;
-- probabilmente, al fine di fare i "calcoli" una sola volta, utilizzerei una CTE di pre-calcolo unificata per poi effettuare la rotazione in colonna nella proiezione successiva
WITH cteAggregate AS (
SELECT g.[data] --, g.[username]
, g.[eventType]
, g.[ore] * 60.0 + g.[minuti] AS [value]
FROM @mainGrid g
WHERE DATEPART(YY, g.[data]) = @year
AND DATEPART(M, g.[data]) = @month
AND g.[username] = @userName
AND g.[eventType] IN ('Change', 'Service request', 'Servizio')
)
SELECT g.[data] --, g.[username]
, SUM(
CASE WHEN g.[eventType] = 'Change' THEN g.[value] ELSE 0 END
) AS [Change]
, SUM(
CASE WHEN g.[eventType] = 'Service request' THEN g.[value] ELSE 0 END
) AS [Service request]
, SUM(
CASE WHEN g.[eventType] = 'Servizio' THEN g.[value] ELSE 0 END
) AS [Servizio]
FROM cteAggregate g
GROUP BY g.[data]
--, g.[username]
;
--<----------
data Change Service request Servizio
---------- ---------- ----------------- ---------
2020-01-01 30.0 90.0 30.0
2020-01-02 90.0 0.0 90.0
2020-01-03 150.0 30.0 0.0
2020-01-04 210.0 270.0 0.0
2020-01-05 0.0 330.0 150.0
2020-01-06 0.0 390.0 270.0
saro' poi molto "old-style", ma l'utilizzo dell' immediate-if (IIF) non riesco ancora a farmelo piacere in T-SQL, ma e' un gusto personale
salutoni
--
Andrea