CREATE TABLE #sum
([Code] int, [Name] varchar(8), [Store] varchar(2), [Balance] int)
;
INSERT INTO #sum
([Code], [Name], [Store], [Balance])
VALUES
(56, 'product1', 'A1', 10),
(56, 'product1', 'B', 100),
(56, 'product1', 'C', 1000),
(56, 'product1', 'D1', 10000),
(56, 'product1', 'A2', 10),
(56, 'product1', 'A3', 10),
(56, 'product1', 'D2', 10000),
(57, 'product2', 'A1', 10),
(57, 'product2', 'B', 100),
(57, 'product2', 'C', 1000),
(57, 'product2', 'D1', 10000),
(57, 'product2', 'A2', 10),
(57, 'product2', 'A3', 10),
(57, 'product2', 'D2', 10000),
(58, 'product3', 'A1', 10),
(58, 'product3', 'B', 100),
(58, 'product3', 'C', 1000),
(58, 'product3', 'D1', 10000),
(58, 'product3', 'A2', 10),
(58, 'product3', 'A3', 10),
(58, 'product3', 'D2', 10000)
;
select Code,Name,LEFT(store,1)store ,SUM(balance) from #sum
group by Code,Name,LEFT(store,1)