首页 新闻 赞助 找找看

Oracle分组提取数据

0
悬赏园豆:5 [已解决问题] 解决于 2012-12-07 09:56

订单表

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(订单数量)

liu@163.com         3

test@126.com   2

我只能拿到每个月的订单数量,我想统计某个用户的,一次拿出来 应该怎么写,如何合并两个表的数据,请教高人。

ruonanxiao的主页 ruonanxiao | 初学一级 | 园豆:6
提问于:2012-12-07 09:34
< >
分享
最佳答案
0

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;

收获园豆:5
samwinchester | 菜鸟二级 |园豆:207 | 2012-12-07 09:55
其他回答(1)
0

select  userId,count(orderNo), max(orderTime) from xx group by userId

向往-SONG | 园豆:4853 (老鸟四级) | 2012-12-07 09:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册