A
A
Anton Artyomov2015-10-24 15:10:45
Oracle
Anton Artyomov, 2015-10-24 15:10:45

How to group by three fields in SQL Oracle if you need to get 10 fields in SELECT?

It is necessary in the query, where 10 fields are selected in SELECT, to group by 3 fields in order to count the number of record clones for these three fields. Is it in principle possible?
Of course, I understand that Oracle will not understand from which record to display data on fields that are not in group by, and therefore swears. For example, MySQL does not pay attention to such a thing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksey Ratnikov, 2015-10-24
@ArtyomovAnton

MySQL is the only DBMS that does this wrong, and this is cured by setting sql_mode to ONLY_FULL_GROUP_BY. Wrap GROUP BY in a subquery and select the missing fields with an external SELECT, or use window functions :

SELECT 
    name,
    lastname,
    count(*) OVER (PARTITION BY group_col1, group_col2, group_col3),
FROM table

In SQL Server, this construction works, in Oracle, in theory, it should too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question