select T0.AcctCode AcctCode1,T0.U_PSTYPE,T0.CogsAcct,SUM(T0.LineTotal) as PSTotal,SUM(T0.StockValue) as PSCogsTotal,SUM(T0.LineTotal)-SUM(T0.StockValue) as RateTotal," & "CASE when T0.U_PSTYPE='返利订单' then '1132001' when T0.U_PSTYPE='随货搭赠订单' then '1132002' when T0.U_PSTYPE='促销订单' and T2.ItmsGrpCod='102' then '650501010104' when T0.U_PSTYPE='促销订单' and T2.ItmsGrpCod='103' then '650501010204' when T0.U_PSTYPE='促销订单' and T2.ItmsGrpCod='100' then '650501010104' when T0.U_PSTYPE='促销订单' and T2.ItmsGrpCod='118' then '650501010304' when T0.U_PSTYPE='费用订单' and T2.ItmsGrpCod='102' then '650501010105' when T0.U_PSTYPE='费用订单' and T2.ItmsGrpCod='103' then '650501010205' when T0.U_PSTYPE='费用订单' and T2.ItmsGrpCod='100' then '650501010105' when T0.U_PSTYPE='费用订单' and T2.ItmsGrpCod='118' then '650501010305' when T0.U_PSTYPE='广告品订单' then '650501010110' when T0.U_PSTYPE='福利订单' then '6602010301' when T0.U_PSTYPE='样品订单' then '650501010112' else '' end as AcctCode," & "CASE WHEN T0.U_PSTYPE IN ('返利订单','随货搭赠订单','促销订单') and T2.ItmsGrpCod='100' then '6505010304' WHEN T0.U_PSTYPE IN ('返利订单','随货搭赠订单','促销订单') and T2.ItmsGrpCod='102' then '6505010301' WHEN T0.U_PSTYPE IN ('返利订单','随货搭赠订单','促销订单') and T2.ItmsGrpCod='118' then '6505010303'WHEN T0.U_PSTYPE IN ('返利订单','随货搭赠订单','促销订单') and T2.ItmsGrpCod='103' then '6505010302' else '' end as RateAcct " & " from INV1 T0 INNER JOIN OINV T1 ON T0.DocEntry=T1.DocEntry INNER JOIN OITM T2 ON T0.ItemCode=T2.ItemCode WHERE T1.DocNum = " & docnum & " and T0.U_PSTYPE IN ('返利订单','随货搭赠订单','促销订单','广告品订单','福利订单','样品订单') group by T0.U_PSTYPE,T0.CogsAcct,T2.ItmsGrpCod,T0.AcctCode"