When I sum a column, I get a single result of all rows summed up. When I group by on this summed column, I get returned multiple rows. Can someone explain what is going on? Is it secretly grouping all the rows with data that is the same and summing these all up first as separate rows?
Table Column Data is
9.75
2.25
8.25
8.25
7
8
5
10.5
5
10.5
10.5
10.25
10.5
10.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
1.5
5
1.75
0.5
5
5
0.5
1.5
5
1.75
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
5
5
0.5
1.75
5
5
0.5
5
10
10
10
10
5
0.5
5
5
0.5
1.75
5
1.5
0.5
5
5
0.5
5
5
0.5
NULL
The Result I get from "Group By column"
Select Sum(column) as [column]
From Table
Group By column
(Result)
NULL
13.5
4.5
7
2.25
265
7
8
16.5
9.75
40
10.25
52.5
The result from just the Sum
Select Sum(column) as [column]
From Table
(result)
436.25
count
, avg
, and sum
are all aggregate functions . They take a group of rows and aggregate them together into one value. count
gives the number of rows, avg
gives their mean, and sum
sums them up.
By default all rows are aggregated together. group by
allows you to split things up into more groups. When you group by column
you say to aggregate together all the rows with the same value for column
. So you're getting the sums of all rows with the same value for column
.
You can see this by also selecting the column and the count.
select column, count(column), sum(column)
from table
group by column
You'll find that sum(column)
is column
times count(column)
. For example.
column, count(column), sum(column)
1.5 3 4.5
8 1 8
10 4 40
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.