订单表
orderNo userId orderTime
1 test@126.com 2012-12-05
2 liu@163.com 2012-11-06
3 liu@163.com 2012-11-06
4 liu@163.com 2012-11-06
5 test@126.com 2012-12-05
用户表
userId userName
test@126.com 测试用户1
liu@163.com 刘生
我想要的效果
userId orderNum(订单数量) orderTime
liu@163.com 3 2012-11-06
test@126.com 2 2012-12-05
结果按orderTime升序.
统计每月 每个用户的订单数量。
我现在可以拿到
select count(orderNo) 订单数量,
to_char(OrderTime,'yyyy')||'-'||to_char(OrderTime,'MM') monthly
from Orders
group by to_char(OrderTime,'yyyy')||'-'||to_char(OrderTime,'MM') ;
userId orderNum(订单数量)
2012-11-6 3
2012-12-05 2
或者可以拿到
select count(OrderNo),UserId from users u right join orders o on u.userId=o.UserId group by o.UserId;
UserId OrderNum(订单数量)
我只能拿到每个月的订单数量,我想统计某个用户的,一次拿出来 应该怎么写,如何合并两个表的数据,请教高人。
select count(orderNo) 订单数量,UserId,
to_char(OrderTime,'yyyy')||'-'||to_char(OrderTime,'MM') monthly
from users u right join orders o on u.userId=o.UserId
group by to_char(OrderTime,'yyyy')||'-'||to_char(OrderTime,'MM') order by to_char(OrderTime,'yyyy') asc;
select userId,count(orderNo), max(orderTime) from xx group by userId