SELECT #_columnNames# , SUM(Quantity) QuantityMTY, SUM(CAST(NetWeight as Decimal)) WeightMTY, SUM(CAST(LineNetAmount as Decimal)) AmountMTY, 0 QuantityMLY, 0.0 WeightMLY, 0.0 AmountMLY, 0 QuantityYTY, 0.0 WeightYTY, 0.0 AmountYTY, 0 QuantityYLY, 0.0 WeightYLY, 0.0 AmountYLY FROM qcf_orderDetail WHERE #_DateField# >= AND #_DateField# <= GROUP BY #_columnNames# SELECT #_columnNames# , 0 QuantityMTY, 0.0 WeightMTY, 0.0 AmountMTY, SUM(Quantity) QuantityMLY, SUM(CAST(NetWeight as Decimal)) WeightMLY, SUM(CAST(LineNetAmount as Decimal)) AmountMLY, 0 QuantityYTY, 0.0 WeightYTY, 0.0 AmountYTY, 0 QuantityYLY, 0.0 WeightYLY, 0.0 AmountYLY FROM qcf_orderDetailLY WHERE #_DateField# >= AND #_DateField# <= GROUP BY #_columnNames# SELECT #_columnNames# , 0 QuantityMTY, 0.0 WeightMTY, 0.0 AmountMTY, 0 QuantityMLY, 0.0 WeightMLY, 0.0 AmountMLY, SUM(Quantity) QuantityYTY, SUM(CAST(NetWeight as Decimal)) WeightYTY, SUM(CAST(LineNetAmount as Decimal)) AmountYTY, 0 QuantityYLY, 0.0 WeightYLY, 0.0 AmountYLY FROM qcf_orderDetail WHERE #_DateField# >= AND #_DateField# <= GROUP BY #_columnNames# SELECT #_columnNames# , 0 QuantityMTY, 0.0 WeightMTY, 0.0 AmountMTY, 0 QuantityMLY, 0.0 WeightMLY, 0.0 AmountMLY, 0 QuantityYTY, 0.0 WeightYTY, 0.0 AmountYTY, SUM(Quantity) QuantityYLY, SUM(CAST(NetWeight as Decimal)) WeightYLY, SUM(CAST(LineNetAmount as Decimal)) AmountYLY FROM qcf_orderDetailLY WHERE #_DateField# >= AND #_DateField# <= GROUP BY #_columnNames# SELECT #_columnNames# ,QuantityMTY ,CAST(WeightMTY as Decimal) WeightMTY ,CAST(AmountMTY as Decimal) AmountMTY ,0 QuantityMLY ,CAST( 0 as Decimal) WeightMLY ,CAST( 0 as Decimal) AmountMLY ,0 QuantityYTY ,CAST( 0 as Decimal) WeightYTY,CAST( 0 as Decimal) AmountYTY ,0 QuantityYLY ,CAST( 0 as Decimal) WeightYLY,CAST( 0 as Decimal) AmountYLY FROM qmd_TYmonth UNION SELECT #_columnNames# ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) ,QuantityMLY ,CAST(WeightMLY as Decimal) WeightMLY ,CAST(AmountMLY as Decimal) AmountMLY ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) FROM qmd_LYmonth UNION SELECT #_columnNames# ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) ,QuantityYTY ,CAST(WeightYTY as Decimal) WeightYTY ,CAST(AmountYTY as Decimal) AmountYTY ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) FROM qmd_TYyear UNION SELECT #_columnNames# ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) ,0 ,CAST( 0 as Decimal) ,CAST( 0 as Decimal) ,QuantityYLY ,CAST(WeightYLY as Decimal) WeightYLY ,CAST(AmountYLY as Decimal) AmountYLY FROM qmd_LYyear SELECT #_columnNames# , SUM(QuantityMTY) QuantityMTY, SUM(CAST(WeightMTY as Decimal)) WeightMTY, SUM(CAST(AmountMTY as Decimal)) AmountMTY, SUM(QuantityMLY) QuantityMLY, SUM(CAST(WeightMLY as Decimal)) WeightMLY, SUM(CAST(AmountMLY as Decimal)) AmountMLY, SUM(QuantityYTY) QuantityYTY, SUM(CAST(WeightYTY as Decimal)) WeightYTY, SUM(CAST(AmountYTY as Decimal)) AmountYTY, SUM(QuantityYLY) QuantityYLY, SUM(CAST(WeightYLY as Decimal)) WeightYLY, SUM(CAST(AmountYLY as Decimal)) AmountYLY FROM qmd_Union GROUP BY #_columnNames# ORDER BY #_columnNames#