views:

26

answers:

4

I've got two SQL Server 2005 tables: MainTable and Orders.

MainTable:

| MainID | Serial |
-------------------
| 1      | A00001 |
| 2      | B00002 |

Orders:

| OrderID | MainID | Name | Value |
-----------------------------------
| 1       | 2      | John | 100   |
| 2       | 2      | Mike | 200   |
| 3       | 1      | John | 150   |
| 4       | 1      | Mike | 350   |
| 5       | 1      | John | 200   |
| 6       | 2      | John | 500   |
| 7       | 1      | Mike | 50    |

I want to get something like this:

|Serial  | Name | Total |
-----------------------
| A00001 | John | 350   |
| A00002 | John | 600   |
| A00001 | Mike | 400   |
| A00002 | Mike | 200   |
+1  A: 
select serial, name, sum(value) as total
from maintable m inner join orders o on
m.mainID = o.mainID
group by
serial, name
Beth
Thank you too!!!
milo2010
+1  A: 
SELECT 
    m.serial,
    o.name,
    SUM(o.value)
FROM 
    main m
    INNER JOIN order o ON m.mainid = o.mainid
GROUP BY
    o.name, 
    m.serial
Andy Evans
That's the way to format SQL! +1
Helgi Hrafn Gunnarsson
Thanks! It works just fine..
milo2010
+1  A: 
SELECT 
M.SERIAL, O.NAME, SUM(VALUE) AS TOTAL
FROM MAINTABLE M JOIN ORDERS O ON O.MAINID=M.MAINID
GROUP BY M.SERIAL, O.NAME
Parkyprg
Thank you too!!!
milo2010
A: 

select Serial , name , Total

from MainTable m,

( select MainID ,name, SUM(value) Total from Orders o group by MainID , Name ) O

where m.MainID= O.MainID

Hasu