Question : Oracle SQL GROUP BY

What is the best approach of the following SQL?

-- Is it gonna give two different results?
-- It makes no difference.
-- How does the oracle process these SQL statements.
-- How exactly does the group by works?

Any thoughts???
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
select 
a.col1,
b.col2,
b.col3,
(case when b.sal > 0 and b.sal2 is not null then 1
     when b.sal < 0 b.sal2 is not null then -1
     else 0
end) sal,
sum(a.col4) sal_2,
sum(a.col5) sal_3

from a, b
where a.col_009 = b.col_008
group by

a.col1,
b.col2,
b.col3,
(case when b.sal > 0 and b.sal2 is not null then 1
     when b.sal < 0 b.sal2 is not null then -1
     else 0
end)


-----------------------OR----------------------

select 
a.col1,
b.col2,
b.col3,
(case when b.sal > 0 and b.sal2 is not null then 1
     when b.sal < 0 b.sal2 is not null then -1
     else 0
end) sal,
sum(a.col4) sal_2,
sum(a.col5) sal_3

from a, b
where a.col_009 = b.col_008
group by

a.col1,
b.col2,
b.col3,
b.sal,
b.sal2

Answer : Oracle SQL GROUP BY

yes you should get different results

yes it makes a difference

I don't know what you're asking.  If you do a an EXPLAIN PLAN for them you can see the execution steps, if you're looking for more details about the internal operations of Oracle query processing, probably can't answer


Group By is just like it sounds,  it divides your data into groups and then performs the aggregates (in your case SUM) on those groups.
That's why your results will be different,  your data is divided differently.  
In the first your sal columns are lumped into 3 categories by your CASE and you are grouping by those 3 categories
In the second, every combination of a.sal and b.sal produces a different group
Random Solutions  
 
programming4us programming4us