on 01-18-2023 10:58 AM
Hello experts!
I'm facing a problem in ABAP SQL, and don't know, whether it's some kind of undocumented feature, or is my database system doing something strange. The issue can be observed by selecting data with GROUP BY addition, but without ORDER BY addition.
Here's my code:
SELECT tabclass,
MIN( as4date ) AS as4date_min,
MAX( as4date ) AS as4date_max
FROM dd02l
INTO TABLE @DATA(g_dd02l)
WHERE tabname LIKE 'DD%'
GROUP BY tabclass.
In my 7.40 systems: SP09 and SP12 with MaxDB the results here are as expected: four lines in alphabetical order: APPEND, INTTAB, POOL (only by SP12), TRANSP, VIEW.
But if I change the code slightly:
SELECT tabclass
FROM dd02l
INTO TABLE @DATA(g_dd02l)
WHERE tabname LIKE 'DD%'
GROUP BY tabclass.
I get different order, namely POOL (only SP12), VIEW, APPEND, INTTAB, TRANSP. Why?
I have checked the same in customer systems: 7.40 SP14 with DB6, 7.50 SP22 with DB6 and 7.50 SP23 with ORACLE, and there is no difference between those two codes.
On the other hand, in customer system 7.52 SP09 with MaxDB the results are same as in my both systems. I thought, it would be a MaxDB issue. But then I came across customer system 7.31 SP29 with DB400. Here the results, for both codes, are as follows: VIEW, INTTAB, APPEND, POOL, TRANSP.
What is the reason for this sort order? Maybe the sort order by using GROUP BY is not given, just like by DISTINCT. But by DISTINCT it's explicitly documented, and by GROUP BY ìt's not. Why does the sort order depend on whether aggregate functions are used or not? Any clues?
Hello michalbadura
As per documentation to SELECT - ORDER BY
If the addition ORDER BY is not specified, the order of all the columns in the results set is undefined.
You are not using ORDER BY hence the order of the result is undefined, i.e. it can be different depending on database, number of calls etc. You must not rely on the order of the result if you do not specify ORDER BY.
Best regards
Dominik Tylczynski
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi dominik-tylczynski, thank You for Your answer.
Please don't misunderstand me - I think You're right. But on the other hand it somehow does not fit to the rest of the pieces. What I mean:
User | Count |
---|---|
75 | |
9 | |
8 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.