tdjfdjdj
tdjfdjdj

Reputation: 2471

Select Sum and multiple columns in 1 select statement

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

Upvotes: 0

Views: 19256

Answers (4)

Vadim Loboda
Vadim Loboda

Reputation: 3101

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;

Upvotes: 3

OCary
OCary

Reputation: 3311

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

Upvotes: 0

skazska
skazska

Reputation: 421

try add GROUP BY

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

Upvotes: 2

Francisco Soto
Francisco Soto

Reputation: 10392

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;

Upvotes: 0

Related Questions