求大神出现指点一二 sql查询问题

bluefoxn 2012-03-28 05:55:42
订单表order
字段:order_id,user_id,order_total,cdate
统计:在某个时间段内用户重复购买的次数(大于等于2才算重复购买),
这里有种情况要考虑进去,比如2012-03-01至2012-03-31之间,user1只买了一次,而在2012-03-01之前购物买过这次也算重复购买
...全文
81 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicenight 2012-03-30
  • 打赏
  • 举报
回复
有点明白了,那就分两次统计做一个相减吧。先统计你要的区间内的数据,再减去你这个区间之前的统计数据。因为你的示例数据都是 27 号的,没啥特殊的结果出现。
另外对你这个逻辑还是有点疑问,如果之前的总消费数据比你选定区间的数据还要多,算出来不就出现负数了?
这只是一个思路吧,你自己改改。

mysql> set @start_time :=  unix_timestamp("2012-03-01");
Query OK, 0 rows affected (0.00 sec)

mysql> set @end_time := unix_timestamp("2012-03-31");
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select date, between_stat.user_id, between_stat.total - ifnull(before_sta
t.total, 0) as '重复次数', between_stat.total as '总共次数', between_stat.total_
cost - ifnull(before_stat.total_cost, 0) as '重复购买金额'
-> from
-> (
-> select
-> concat(date_format(from_unixtime(min(cdate)), "%Y-%m-%d"), "至",
date_format(from_unixtime(max(cdate)), "%Y-%m-%d")) as date,
-> user_id,
-> count(user_id) as total,
-> sum(order_total) as total_cost
-> from orders
-> where cdate between @start_time and @end_time
-> group by user_id
-> ) as between_stat
->
-> left join
->
-> (
-> select
-> user_id,
-> count(user_id) as total,
-> sum(order_total) as total_cost
-> from orders
-> where cdate < @start_time
-> group by user_id
-> ) as before_stat
->
-> on between_stat.user_id = before_stat.user_id;
+------------------------+---------+----------+----------+--------------+
| date | user_id | 重复次数 | 总共次数 | 重复购买金额 |
+------------------------+---------+----------+----------+--------------+
| 2012-03-27至2012-03-27 | 1 | 2 | 2 | 105.99000 |
| 2012-03-27至2012-03-27 | 3 | 3 | 3 | 89.00000 |
| 2012-03-27至2012-03-27 | 5 | 1 | 1 | 36.00000 |
| 2012-03-27至2012-03-27 | 64 | 2 | 2 | 231.03000 |
+------------------------+---------+----------+----------+--------------+
4 rows in set (0.00 sec)

mysql>
nicenight 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

count(*)不能用在where里呀? 谁有什么好办法
[/Quote]

count 用在 having 里。

[Quote=引用楼主 的回复:]
订单表order
字段:order_id,user_id,order_total,cdate
统计:在某个时间段内用户重复购买的次数(大于等于2才算重复购买),
这里有种情况要考虑进去,比如2012-03-01至2012-03-31之间,user1只买了一次,而在2012-03-01之前购物买过这次也算重复购买
[/Quote]

最后一句话不理解,这里给一个思想吧,自己改:

mysql> select
-> concat(date_format(from_unixtime(min(cdate)), "%Y-%m-%d"), "至", date
_format(from_unixtime(max(cdate)), "%Y-%m-%d")) as data,
-> user_id,
-> count(user_id) - 1 as '重复次数',
-> count(user_id) as '总数',
-> sum(order_total) as '重复购买金额'
-> from orders
-> group by user_id
-> having count(user_id) > 1;
+------------------------+---------+----------+------+--------------+
| data | user_id | 重复次数 | 总数 | 重复购买金额 |
+------------------------+---------+----------+------+--------------+
| 2012-03-27至2012-03-27 | 1 | 1 | 2 | 105.99000 |
| 2012-03-27至2012-03-27 | 3 | 2 | 3 | 89.00000 |
| 2012-03-27至2012-03-27 | 64 | 1 | 2 | 231.03000 |
+------------------------+---------+----------+------+--------------+
3 rows in set (0.00 sec)
useky 2012-03-29
  • 打赏
  • 举报
回复
count(*)不能用在where里呀? 谁有什么好办法
小小小小蜗牛 2012-03-29
  • 打赏
  • 举报
回复
顶起 我们都一样 好好学习 还是好孩子
ACMAIN_CHM 2012-03-29
  • 打赏
  • 举报
回复
我的理解能力仍不足以看出结果是如何从楼主提供的测试数据中得来,那个日期已经让人头晕了。
wolfwu_kg5 2012-03-29
  • 打赏
  • 举报
回复
表格中的cdate是干嘛的 还有重复购买金额是怎么算出来的?
bluefoxn 2012-03-29
  • 打赏
  • 举报
回复
Mysql 数据库

CREATE TABLE IF NOT EXISTS `orders` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL DEFAULT '0',
`order_total` decimal(15,5) NOT NULL DEFAULT '0.00000',
`cdate` int(11) DEFAULT NULL,
PRIMARY KEY (`order_id`)
)

INSERT INTO `orders` (`order_id`, `user_id`, `order_total`, `cdate`) VALUES
(1, 64, '137.04', 1332818403),
(2, 3, '58.00', 1332817679),
(3, 64, '93.99', 1332816160),
(4, 1, '93.99', 1332816039),
(5, 3, '22.00', 1332812556),
(6, 5, '36.00', 1332809122),
(7, 1, '12.00', 1332806097),
(8, 3, '9.00', 1332804724),

要得到这样的报表:
Date User_id 重复次数 总共次数 重复购买金额
2012-03-01至2012-03-16 64 1 2 93.99000
2012-03-01至2012-03-16 3 2 3 80.99000
bluefoxn 2012-03-29
  • 打赏
  • 举报
回复
汇总的SQL也有请教下:

+------------------------+------+--------------+------+
| date | 重复次数 | 总数 | 重复购买金额 |
+------------------------+------+--------------+------+
| 2012-03-27至2012-03-27 | 4 | 7 | 426.02000 |
+------------------------+------+--------------+------+
bluefoxn 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nicenight 的回复:]

引用 6 楼 的回复:

count(*)不能用在where里呀? 谁有什么好办法


count 用在 having 里。

引用楼主 的回复:
订单表order
字段:order_id,user_id,order_total,cdate
统计:在某个时间段内用户重复购买的次数(大于等于2才算重复购买),
这里有种情况要考虑进去,比如2012-03-01至2012-0……
[/Quote]

谢谢您提出来的思路,最后一句话的意思是统计某个时间范围内,要把它之前是否有购买过产品的情况也考虑进去,如果它之前没有买的话则重复数就等于这个时间范围内的次数-1,如果有买的话就不用-1
ACMAIN_CHM 2012-03-28
  • 打赏
  • 举报
回复
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

56,675

社区成员

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

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