Select Sum and multiple columns in 1 select statem

2019-02-16 03:20发布

Is there a way to select the sum of a column and other columns at the same time in SQL?

Example:

SELECT sum(a) as car,b,c FROM toys

4条回答
Ridiculous、
2楼-- · 2019-02-16 03:42

Since you do not give much context, I assume you mean one of the following :

SELECT (SELECT SUM(a) FROM Toys) as 'car', b, c FROM Toys;

or

SELECT SUM(a) as Car, b, c FROM Toys GROUP BY b, c;
查看更多
何必那么认真
3楼-- · 2019-02-16 03:45

try add GROUP BY

SELECT sum(a) as car,b,c FROM toys 
GROUP BY b, c
查看更多
狗以群分
4楼-- · 2019-02-16 03:45

How about:

select sum(a) over(), b, c from toy;

or, if it's required:

select sum(a) over(partition by b), b, c from toy;
查看更多
祖国的老花朵
5楼-- · 2019-02-16 03:53
SELECT b,
 c,
 (SELECT sum(a) FROM toys) as 'car'
FROM toys
查看更多
登录 后发表回答