计数查询统计

CREATE TABLE `statistic_order` (
  `oid` bigint(20) NOT NULL,
  `o_source` varchar(25) DEFAULT NULL COMMENT '来源编号',
  `o_actno` varchar(30) DEFAULT NULL COMMENT '活动编号',
  `o_actname` varchar(100) DEFAULT NULL COMMENT '参与活动名称',
  `o_n_channel` int(2) DEFAULT NULL COMMENT '商城平台',
  `o_clue` varchar(25) DEFAULT NULL COMMENT '线索分类',
  `o_star_level` varchar(25) DEFAULT NULL COMMENT '订单星级',
  `o_saledep` varchar(30) DEFAULT NULL COMMENT '营销部',
  `o_style` varchar(30) DEFAULT NULL COMMENT '车型',
  `o_status` int(2) DEFAULT NULL COMMENT '订单状态',
  `syctime_day` varchar(15) DEFAULT NULL COMMENT '按天格式化日期',
  PRIMARY KEY (`oid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


select S.syctime_day,
   sum(case when S.o_source = 'CDE' then 1 else 0 end) as 'CDE',
   sum(case when S.o_source = 'SDE' then 1 else 0 end) as 'SDE',
   sum(case when S.o_source = 'PDE' then 1 else 0 end) as 'PDE',
   sum(case when S.o_source = 'CSE' then 1 else 0 end) as 'CSE',
   sum(case when S.o_source = 'SSE' then 1 else 0 end) as 'SSE'
 from statistic_order S where S.syctime_day > '2015-05-01' and S.syctime_day < '2016-08-01' 
 GROUP BY S.syctime_day order by S.syctime_day asc;

计数查询

SELECT r.realname ,r.start_time,r.end_time,
sum(case when r.dintype=1 then 1 else 0 end) as 'zhong',
sum(case when r.dintype=2 then 1 else 0 end) as 'wan'
from reports r where r.start_time<'当前时间' and r.end_time<'2018-08-01' GROUP by r.realname,r.start_time,r.end_time ORDER by r.realname;

posted @ 2018-05-04 19:19  亦星  阅读(170)  评论(0编辑  收藏  举报