用sql写小计和合计

来源:互联网 发布:幸福狐狸内衣淘宝有卖 编辑:程序博客网 时间:2024/05/16 15:22

select case when b.name is null and c.name is null then '合计' when b.name is null and c.name is not null then '小计' else b.name end as mtrname,
sum(a.number),c.name as cname from x_sell a join x_material b on a.mtr=b.fid join p_organi c on c.fid=a.customer
where a.stime>'2009-10-01'
GROUP BY c.name,b.name WITH ROLLUP
order by c.name desc,b.name desc