生成报表的神器sql

 

找了老半天总算是找到这个函数了,有三年没有动过这个东西了,最近写点东西需要用到报表中统计各行各列的总计,想起来以前写过,可是死活想不起来怎么写,网上也不知道怎么查,回头看看三年前的某代码,总算找到了,先mark一下

 

SELECT SQL_BUFFER_RESULT GOODS.GOODSNAME AS '???????',IFNULL(SUM(hisgoods.Number),0) AS '????????',IFNULL(SUM(hisgoods.Number * hisgoods.Price),0) AS '???????'  
           FROM goods LEFT JOIN hisgoods  ON goods.GoodsName = HISGOODS.GOODSNAME 
           where date(CLTime)= date(CLTIME1) and hisgoods.CaoZuoLeiXing = '????'
           GROUP BY goods.GoodsName WITH ROLLUP; 

 乱码是什么就不重要的,主要还是报表列名称了,重点是要mark一下With RollUp

 

 

现在想得到每个班级的前2名学生资料

SQL:

SELECT a.* FROM student a WHERE (SELECT COUNT(*) FROM student WHERE class = a.class AND id < a.id ) < 2 ORDER BY a.class,a.id;

 

 

按name分组取最大的两个(N个)val

 

select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val

你可能感兴趣的:(WITH ROLLUP)