SELECT
speApp1.dbo.tb_product.goodid
,dbwins_new1.dbo.emgood.goodname1,
SUM(CASE month(soinvhd.docudate) WHEN '1' THEN goodqty2 ELSE 0 END) AS M01,
SUM(CASE month(soinvhd.docudate) WHEN '2' THEN goodqty2 ELSE 0 END) AS M02,
SUM(CASE month(soinvhd.docudate) WHEN '3' THEN goodqty2 ELSE 0 END) AS M03,
SUM(CASE month(soinvhd.docudate) WHEN '4' THEN goodqty2 ELSE 0 END) AS M04,
SUM(CASE month(soinvhd.docudate) WHEN '5' THEN goodqty2 ELSE 0 END) AS M05,
SUM(CASE month(soinvhd.docudate) WHEN '6' THEN goodqty2 ELSE 0 END) AS M06,
SUM(CASE month(soinvhd.docudate) WHEN '7' THEN goodqty2 ELSE 0 END) AS M07,
SUM(CASE month(soinvhd.docudate) WHEN '8' THEN goodqty2 ELSE 0 END) AS M08,
SUM(CASE month(soinvhd.docudate) WHEN '9' THEN goodqty2 ELSE 0 END) AS M09,
SUM(CASE month(soinvhd.docudate) WHEN '10' THEN goodqty2 ELSE 0 END) AS M10,
SUM(CASE month(soinvhd.docudate) WHEN '11' THEN goodqty2 ELSE 0 END) AS M11,
SUM(CASE month(soinvhd.docudate) WHEN '12' THEN goodqty2 ELSE 0 END) AS M12
from
speApp1.dbo.tb_product
left outer join dbwins_new1.dbo.emgood on speApp1.dbo.tb_product.goodid =dbwins_new1.dbo.emgood.goodid
left outer join dbwins_new1.dbo.soinvdt on dbwins_new1.dbo.emgood.goodid =dbwins_new1.dbo.soinvdt.goodid
left outer join dbwins_new1.dbo.soinvhd on dbwins_new1.dbo.soinvdt.soinvid =dbwins_new1.dbo.soinvhd.soinvid
where
(
(dbwins_new1.dbo.soinvhd.docutype=107 and dbwins_new1.dbo.soinvhd.docustatus<>'C')
OR
(dbwins_new1.dbo.soinvhd.docutype=108 and dbwins_new1.dbo.soinvhd.transpid=17001)
)
AND YEAR(dbwins_new1.dbo.soinvhd.docudate) = 2010
and speApp1.dbo.tb_product.IDUser=3
and speApp1.dbo.tb_product.ID_Folder=1
group by speApp1.dbo.tb_product.goodid
,dbwins_new1.dbo.emgood.goodname1
order by speApp1.dbo.tb_product.goodid asc
(
(dbwins_new1.dbo.soinvhd.docutype=107 and dbwins_new1.dbo.soinvhd.docustatus<>'C')
OR
(dbwins_new1.dbo.soinvhd.docutype=108 and dbwins_new1.dbo.soinvhd.transpid=17001)
)