Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

For example I have a table like this:

product | quantity | something
-------------------------------
 abc    |   5      |  2
 xzy    |   5      |  2
 asd    |   10     |  2

When I need all record I do like this and get 3 results:

select * from tableName 

But I want get a 4th row for total of quantity(50) and something(6). Is it possible in sql query or I have to loop my result to get total after query execution? I want to do this in query if possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
292 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use rollup to generate totals, but you have to change it to an aggregate function, like this:

SELECT product, sum(quantity), sum(something)
FROM tableName
GROUP BY product WITH ROLLUP

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...