mysql 分组统计SQL语句

1.按照特定字段;

2.在某一日期范围内;

3.按日、按月统计;

4.动态传入数据库表名称。

select <if test="dateType==&quot;d&quot;">DATE_FORMAT(tj.trans_date,'%Y-%m-%d') date1,</if>
<if test="dateType==&quot;m&quot;">DATE_FORMAT(tj.trans_date,'%Y-%m') date1,</if>
c1.customer_name customerName,p.product_name productName,tj.result result,count(0) tjl,his.run_state runState
from ${tableName} tj,tableName1 c1,tableName2 his,tableName3 p
where c1.customer_id=tj.customer_id and tj.trans_id=his.trans_id and tj.product_id=p.product_id
<if test="startDate!=null&amp;&amp;startDate!=&quot;&quot;"> and DATE_FORMAT(tj.trans_date,'%Y-%m-%d') &gt;= #{startDate}</if>
<if test="endDate!=null&amp;&amp;endDate!=&quot;&quot;"> and DATE_FORMAT(tj.trans_date,'%Y-%m-%d') &lt;= #{endDate}</if>
<include refid="WHERE_CONDITIONS"/>
GROUP BY date1,customerName,result,runState

 

 
posted @ 2016-11-23 15:36  小妮儿玩博客  阅读(1208)  评论(0编辑  收藏  举报