Этого можно достичь, используя max(case when ... end)
функции и группировку по code
, это обычный способ сводить данные в T-SQL:
Пример данных:
create table tbl(date date, code char(10), lc_code int, qty float);
insert into tbl (code,lc_code,qty) values
('MC20651', 1126, 322.00),
('MC10102', 3356, 30.00),
('MC10201', 4422, 56.00),
('MC10303', 5065, 55.00),
('MC20902', 7012, 65.00),
('MC50201', 1258, 45.00),
('MC10201', 1126, 86.00),
('MC50201', 7012, 14.25),
('MC20651', 1258 , 322.00),
('MC20651', 3356, 78.00);
T-SQL:
select `code`
`1126`,
`3356`,
`4422`,
`5065`,
`7012`,
`1258`,
`1126` + `3356` + `4422` + `5065` + `7012` + `1258` `sum`
from (
select `code`,
max(case lc_code when 1126 then qty else 0 end) `1126`,
max(case lc_code when 3356 then qty else 0 end) `3356`,
max(case lc_code when 4422 then qty else 0 end) `4422`,
max(case lc_code when 5065 then qty else 0 end) `5065`,
max(case lc_code when 7012 then qty else 0 end) `7012`,
max(case lc_code when 1258 then qty else 0 end) `1258`
from tbl
group by `code`
) `a`;