$strSQL = "
SELECT dbwins_new1.dbo.emgood.goodname1,speApp1.dbo.tb_product.idprd,dbwins_new1.dbo.emgood.goodid,
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
dbwins_new1.dbo.soinvhd
left outer join dbwins_new1.dbo.soinvdt on dbwins_new1.dbo.soinvhd.soinvid = dbwins_new1.dbo.soinvdt.soinvid
left outer join speApp1.dbo.tb_product on dbwins_new1.dbo.soinvdt.goodid = speApp1.dbo.tb_product.goodid
left outer join dbwins_new1.dbo.emgood on speApp1.dbo.tb_product.goodid = dbwins_new1.dbo.emgood.goodid
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) = $year
and speApp1.dbo.tb_product.IDUser=$IDUser
and speApp1.dbo.tb_product.ID_Folder=$folder
group by dbwins_new1.dbo.soinvdt.goodid,dbwins_new1.dbo.emgood.goodname1,speApp1.dbo.tb_product.idprd,dbwins_new1.dbo.emgood.goodid
order by speApp1.dbo.tb_product.idprd desc
";