-----------table1-------------
id code name quantity
1 001 car1 1
2 002 car2 2
3 003 car3 3
-----------table2-------------
id code name quantity
1 001 car1 1
2 002 car2 2
-----------table3-------------
id code name quantity
1 001 car1 1
2 002 car2 2
3 004 car4 4
我想加盟三个表,并采取总量从数量在SQL Server:
---------table-------
code name total
001 car1 3
002 car2 6
003 car3 3
004 car4 4
在MySQL中我尝试这一点,工作,但在SQL Server中,我得到了我的错误:(
select
ID, CODE, NAME, sum(QUANTITY) as total
from
(select ID, CODE, NAME, QUANTITY from AP1
union all
select ID, CODE, NAME, QUANTITY from AP2
union all
select ID, CODE, NAME, QUANTITY from AP3) x
group by ID;
您可以使用GROUP BY中'SELECT'子句的所有非聚合列,如'group by ID,CODE,NAME'。 –
如果你有时间,给我写个例子,因为我不明白 –
@ConstantinosAggelou - 请看下面的答案。 – GurV