简体   繁体   中英

How can I perform this aggregate?

I have crated two table one is customers and the other one is ord

select * from customers;

    id  Name    age     adress  salary
2   102 jpj     24      zzzz    10000
3   103 ftd     20      kkkk    20000
4   104 jin     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;

oid     order_date              id  amount
201     12/11/2013 1:00:00 AM   102 2500
202     12/11/2013 4:14:17 AM   102 3000
203     12/9/2013 9:18:16 PM    103 2000
204     12/8/2013 12:00:00 PM   102 1000

using the query

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

now its print like

1   jpg     10000   1000
2   jpg     10000   3000
3   jpg     10000   2500
4   ftd     20000   2000
5   vijay   12000   2000

But I would like to print my table

name     id     amount
jpj      102    6500  -> this is the sum of amount in the order table order by 102
ftd      103    2000 
vijay    107    2000

please help me to solve this problem I am a starter in sql

You need to group the data; for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM