我有两个SQL Server 2005表:MainTable和Orders。
MainTable:
| MainID | Serial |
-------------------
| 1 | A00001 |
| 2 | B00002 |
订单:
| 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 |
我想得到这样的东西:
|Serial | Name | Total |
-----------------------
| A00001 | John | 350 |
| A00002 | John | 600 |
| A00001 | Mike | 400 |
| A00002 | Mike | 200 |
答案 0 :(得分:1)
select serial, name, sum(value) as total
from maintable m inner join orders o on
m.mainID = o.mainID
group by
serial, name
答案 1 :(得分:1)
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
答案 2 :(得分:1)
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
答案 3 :(得分:0)
选择序列号,名称,总计
来自MainTable m,
(选择MainID,名称,SUM(值)按订单组的总数o按主题,名称分组)O
其中m.MainID = O.MainID