select * from t_TableA select CASE WHEN GROUPING(Dept)=1 THEN '合计' WHEN GROUPING(Goodscode)=1 THEN '(小计)' ELSE CONVERT(varchar(355),Dept) END, Goodscode, SUM(Amount) AS Amount from t_TableA WHERE 1=1 GROUP BY Dept,goodscode WITH ROLLUP