Я просто пытаюсь сделать то, что вы пытались сделать. Я думаю, вам нужно создать группировку столбцов за месяц. просто попробуйте с приведенным ниже набором данных для вашей ссылки.
CREATE TABLE #TEMP_RAW_DATA
(
N_id NUMERIC(18,0) IDENTITY(1,1),
VC_Country VARCHAR(30),
VC_States VARCHAR(50),
D_Date DATE,
I_Values INT
)
INSERT INTO #TEMP_RAW_DATA
(
VC_Country,VC_States,D_Date,I_Values
)
SELECT 'USA','Alabama','09-01-2019',57 UNION ALL
SELECT 'USA','Alabama','10-01-2019',47 UNION ALL
SELECT 'USA','Alabama','11-01-2019',69 UNION ALL
SELECT 'USA','Alabama','12-01-2019',1 UNION ALL
SELECT 'USA','Alabama','01-01-2020',42 UNION ALL
SELECT 'USA','Hawaii','09-01-2019',80 UNION ALL
SELECT 'USA','Hawaii','10-01-2019',55 UNION ALL
SELECT 'USA','Hawaii','11-01-2019',19 UNION ALL
SELECT 'USA','Hawaii','12-01-2019',73 UNION ALL
SELECT 'USA','Hawaii','01-01-2020',76 UNION ALL
SELECT 'USA','Massachusetts','09-01-2019',20 UNION ALL
SELECT 'USA','Massachusetts','10-01-2019',74 UNION ALL
SELECT 'USA','Massachusetts','11-01-2019',30 UNION ALL
SELECT 'USA','Massachusetts','12-01-2019',36 UNION ALL
SELECT 'USA','Massachusetts','01-01-2020',53 UNION ALL
SELECT 'USA','Pennsylvania','09-01-2019',53 UNION ALL
SELECT 'USA','Pennsylvania','10-01-2019',17 UNION ALL
SELECT 'USA','Pennsylvania','11-01-2019',1 UNION ALL
SELECT 'USA','Pennsylvania','12-01-2019',13 UNION ALL
SELECT 'USA','Pennsylvania','01-01-2020',42 UNION ALL
SELECT 'USA','Virginia','09-01-2019',26 UNION ALL
SELECT 'USA','Virginia','10-01-2019',24 UNION ALL
SELECT 'USA','Virginia','11-01-2019',29 UNION ALL
SELECT 'USA','Virginia','12-01-2019',79 UNION ALL
SELECT 'USA','Virginia','01-01-2020',73
SELECT VC_Country,VC_States,DATENAME(MONTH,D_Date) VC_Month,
CASE
WHEN PATINDEX('[a-f]%',VC_States)>0 THEN 'A-F'
WHEN PATINDEX('[g-n]%',VC_States)>0 THEN 'G-N'
WHEN PATINDEX('[o-z]%',VC_States)>0 THEN 'O-Z'
END VC_Group,
SUM(I_Values) I_Values
FROM #TEMP_RAW_DATA
GROUP BY VC_Country,VC_States,
CASE
WHEN PATINDEX('[a-f]%',VC_States)>0 THEN 'A-F'
WHEN PATINDEX('[g-n]%',VC_States)>0 THEN 'G-N'
WHEN PATINDEX('[o-z]%',VC_States)>0 THEN 'O-Z'
END,DATENAME(MONTH,D_Date)