I have a sidewalk_deficiency
table:
create table sidewalk_deficiency (
def_id number(10),
insp_id number(10),
def_length number(10,2));
insert into sidewalk_deficiency values (10,1,.5);
insert into sidewalk_deficiency values (11,1,1);
insert into sidewalk_deficiency values (12,1,1.5);
insert into sidewalk_deficiency values (13,2,2);
insert into sidewalk_deficiency values (14,2,2.5);
commit;
DEF_ID INSP_ID DEF_LENGTH
---------- ---------- ----------
10 1 .5
11 1 1
12 1 1.5
13 2 2
14 2 2.5
I can create a query that summarizes the sidewalk inspections by tallying up the deficiency lengths, per inspection:
select
insp_id,
sum(def_length) as def_length
from
sidewalk_deficiency
group by
insp_id
INSP_ID DEF_LENGTH
---------- ----------
1 3
2 4.5
This works as I had expected. Two rows are output; one for each insp_id
.
I understand that the above query does, in fact, provide the desired output. However, I don't really understand why it works the way it does.
Admittedly, when I write queries like this, it makes more sense to me to include fields such as def_length
in both the aggregate function and the group by
clause. To me, I know I want to collapse the def_length
into a sum, so it seems to me that I should group by it.
select
insp_id,
sum(def_length) as def_length
from
sidewalk_deficiency
group by
insp_id,
def_length --<<--Problem
INSP_ID DEF_LENGTH
---------- ----------
2 2.5
1 .5
1 1
2 2
1 1.5
Of course, as we can see, my thought process is flawed. When I include the def_length
field in the group by
, it produces more records than I had expected.
Forgive my ignorance, but why does the second query produce more results than the first? I would have thought that including the field in both the aggregate function and the group by
would have grouped the rows as I had planned.
GROUP BY
list. YouGROUP BY date
, you get as many rows as the different dates, youGROUP BY insp_id
, you get as many rows as the differentinsp_id
values. YouGROUP BY a,b
, you get as many rows as the different (a,b)
combinations.group by
and aggregates all in a single 'Totals row', so-to-speak. This had led me to believe that the aggregate was some type ofgroup by
, which is obviously false. Screencap here.