Select Sum and multiple columns in 1 select statement

前端 未结 4 1438
醉酒成梦
醉酒成梦 2021-01-07 14:14

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条回答
  • 2021-01-07 14:50

    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;
    
    0 讨论(0)
  • 2021-01-07 14:55

    try add GROUP BY

    SELECT sum(a) as car,b,c FROM toys 
    GROUP BY b, c
    
    0 讨论(0)
  • 2021-01-07 15:06
    SELECT b,
     c,
     (SELECT sum(a) FROM toys) as 'car'
    FROM toys
    
    0 讨论(0)
  • 2021-01-07 15:17

    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;
    
    0 讨论(0)
提交回复
热议问题