请帮我解决这个问题

时间:2013-12-12 08:52:14

标签: sql oracle

我有两个表,其中一个是cutomer,另一个是ord

select * from customers; 

客户表

1   101 jun 23  yyyy    15000
2   102 jas 24  zzzz    10000
3   103 fat 20  kkkk    20000
4   104 jini    40  llll    30000
5   105 michael 30  dddd    25000
6   106 das 25  hhhh    10000
7   107 vijay   26  mmmm    12000
8   108 thanku  31  jjjj    26000
9   109 vishnu  34  gggg    24000
10  110 vas 28  ffff    18000

select * from ord;

这是订单表

1       12/11/2013 1:00:00 AM   102 2500
2   202 12/11/2013 4:14:17 AM   102 3000
3   203 12/9/2013 9:18:16 PM    103 2000
4   204 12/8/2013 12:00:00 PM   102 1000
5   205 12/24/2013  107 2000

这是我用过的联合命令

select c.name,c.salary,o.amount 
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id;

然后生成的表是

1   jas 10000   1000
2   jas 10000   3000
3   jas 10000   2500
4   fat 20000   2000
5   vijay   12000   2000

我想要这样的结果表

1      jas      10000   6500
2      fat      20000   2000
3      vijay    12000   2000

请帮助我解决这个问题。

4 个答案:

答案 0 :(得分:4)

group by c.name, c.salary sum(salary)是你想要的:

select c.name, c.salary, sum(o.amount )
from CUSTOMERS c
inner join ord o on c.id=o.customer_id
group by c.name, c.salary;

答案 1 :(得分:1)

试试这个是否有用。


    select c.name,c.salary,sum(o.amount)
    from CUSTOMERS c
    inner join ord o
    on c.id=o.customer_id
    group by 1,2;

感谢。

答案 2 :(得分:1)

select c.name,c.salary,SUM(o.amount )
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id
GROUP BY c.name,c.salary

我认为这会起作用

答案 3 :(得分:0)

使用Left JoinRIGHT JOIN

select c.name,c.salary,o.amount 
from CUSTOMERS c
left join ord o
on c.id=o.customer_id;