按日期统计的问题

test119 2009-10-10 09:44:14
表如下
id 业务编号 订购或退订 时间
1 111 1 2009-09-01 12:11:11
2 112 0 2009-09-02 12:11:11
3 111 1 2009-09-03 12:11:11
4 113 0 2009-09-05 12:11:11


统计出每天或者每月每项业务订购多少退订多少,想把数据导入到另一个新表中

时间 订购数 退订数
9月1日 111 55


时间 订购数 退订数
9月 111 55
10月 111 55
...全文
80 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
vinsonshen 2009-10-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 weihongchang 的回复:]
还有按业务分类呢。。
[/Quote]

我前面的语句不是根据你所列的结果写的,而是根据我以前这方面的统计需求经验直接已经帮你按日期、业务分了的
阿_布 2009-10-10
  • 打赏
  • 举报
回复

mysql> select bno,date_format(dd,'%m月%d日') ddate,sum(type) 订购数,
-> count(*)-sum(type) 退订数
-> from orders
-> group by date_format(dd,'%m月%d日'),bno;
+------+----------+--------+--------+
| bno | ddate | 订购数 | 退订数 |
+------+----------+--------+--------+
| 111 | 09月01日 | 1 | 0 |
| 112 | 09月02日 | 0 | 1 |
| 111 | 09月03日 | 1 | 0 |
| 113 | 09月05日 | 0 | 1 |
+------+----------+--------+--------+
4 rows in set (0.00 sec)
阿_布 2009-10-10
  • 打赏
  • 举报
回复

mysql> select bno,date_format(dd,'%m月') ddate,sum(type) 订购数,
-> count(*)-sum(type) 退订数
-> from orders
-> group by date_format(dd,'%m月'),bno;
+------+-------+--------+--------+
| bno | ddate | 订购数 | 退订数 |
+------+-------+--------+--------+
| 111 | 09月 | 2 | 0 |
| 112 | 09月 | 0 | 1 |
| 113 | 09月 | 0 | 1 |
+------+-------+--------+--------+
3 rows in set (0.02 sec)
wwwwb 2009-10-10
  • 打赏
  • 举报
回复
select 业务分类,DATE_FORMAT(时间,'%m-%d'),sum(if(订购或退订=1,1,0)) as 订购数 ,
,sum(if(订购或退订=0,1,0)) as 退订数
from tt group by 业务分类,DATE_FORMAT(时间,'%m-%d')

select 业务分类,DATE_FORMAT(时间,'%m'),sum(if(订购或退订=1,1,0)) as 订购数 ,
,sum(if(订购或退订=0,1,0)) as 退订数
from tt group by 业务分类,DATE_FORMAT(时间,'%m')
test119 2009-10-10
  • 打赏
  • 举报
回复
还有按业务分类呢。。
vinsonshen 2009-10-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 weihongchang 的回复:]
数据可能是05年到现在。。每天都有数据,我的意思是用一条语句实现按月的分组了。。
[/Quote]

你这是SP里面统计需求,以前搞过,基本上像我前面处理就行了。
阿_布 2009-10-10
  • 打赏
  • 举报
回复

mysql> select date_format(dd,'%m月') ddate,sum(type) ord,
-> count(*)-sum(type) back
-> from orders
-> group by date_format(dd,'%m月');
+-------+------+------+
| ddate | ord | back |
+-------+------+------+
| 09月 | 2 | 2 |
+-------+------+------+
1 row in set (0.00 sec)
nianzhang747 2009-10-10
  • 打赏
  • 举报
回复
date_format(dd,'%m月');
test119 2009-10-10
  • 打赏
  • 举报
回复
数据可能是05年到现在。。每天都有数据,我的意思是用一条语句实现按月的分组了。。
阿_布 2009-10-10
  • 打赏
  • 举报
回复

mysql> select * from orders;
+------+------+------+---------------------+
| id | bno | type | dd |
+------+------+------+---------------------+
| 1 | 111 | 1 | 2009-09-01 12:11:11 |
| 2 | 112 | 0 | 2009-09-02 12:11:11 |
| 3 | 111 | 1 | 2009-09-03 12:11:11 |
| 4 | 113 | 0 | 2009-09-05 12:11:11 |
+------+------+------+---------------------+
4 rows in set (0.00 sec)

mysql> select date_format(dd,'%m月%d日') ddate,sum(type) ord,
-> count(*)-sum(type) back
-> from orders
-> group by date_format(dd,'%m月%d日');
+----------+------+------+
| ddate | ord | back |
+----------+------+------+
| 09月01日 | 1 | 0 |
| 09月02日 | 0 | 1 |
| 09月03日 | 1 | 0 |
| 09月05日 | 0 | 1 |
+----------+------+------+
4 rows in set (0.00 sec)
nianzhang747 2009-10-10
  • 打赏
  • 举报
回复
如果按每天和每月的话
sum ... group by
nianzhang747 2009-10-10
  • 打赏
  • 举报
回复
id 业务编号 订购或退订 时间
1 111 1 2009-09-01 12:11:11
2 112 0 2009-09-02 12:11:11
3 111 1 2009-09-03 12:11:11
4 113 0 2009-09-05 12:11:11
--------------------------------------------
create table '订购' as select id,业务编号,订购,时间 from tb where 定购或退定=1;
create table '退定' as select id,业务编号,退定,时间 from tb where 定购或退定=0;
WWWWA 2009-10-10
  • 打赏
  • 举报
回复
select DATE_FORMAT(时间,'%m-%d'),sum(if(订购或退订=1,1,0)) as 订购数 ,
,sum(if(订购或退订=0,1,0)) as 退订数
from tt group by DATE_FORMAT(时间,'%m-%d')

select DATE_FORMAT(时间,'%m'),sum(if(订购或退订=1,1,0)) as 订购数 ,
,sum(if(订购或退订=0,1,0)) as 退订数
from tt group by DATE_FORMAT(时间,'%m')
vinsonshen 2009-10-10
  • 打赏
  • 举报
回复
日的:
select date(时间) as 统计时间,业务编号,sum(if(订购或退订=1,1,0)) as 订购数,sum(if(订购或退订=0,1,0)) as 退订数 from 表 group by date(时间),业务编号

月的:
select date_format(时间,'%Y-%m') as 统计时间,业务编号,sum(if(订购或退订=1,1,0)) as 订购数,sum(if(订购或退订=0,1,0)) as 退订数 from 表 group by date_format(时间,'%Y-%m'),业务编号

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧