views:

920

answers:

2

PL/SQL is not my friend recently. I am getting the above error with the following query:

SELECT cr.review_sk, cr.cs_sk, cr.full_name,
tolist(to_char(cf.fact_date, 'mm/dd/yyyy')) "appt",
cs.cs_id, cr.tracking_number
from review cr, cs, fact cf
where cr.cs_sk = cs.cs_sk
and UPPER(cs.cs_id) like '%' || UPPER(i_cs_id) || '%'
and row_delete_date_time is null
and cr.review_sk = cf.review_wk (+)
and cr.fact_type_code (+) = 183050
GROUP BY cr.review_sk, cr.cs_sk, cf.fact_date, cr.tracking_number
ORDER BY cs.cs_id, cr.full_name;

I couldn't find any examples that had both group by and order by clauses in the same query. I tried removing each field from the group by one at a time, but am still getting the same error.

+2  A: 

Include in the GROUP BY clause all SELECT expressions that are not group function arguments.

Xaisoft
+7  A: 

You must put all columns of the SELECT in the GROUP BY or use functions on them which compress the results to a single value (like MIN, MAX or SUM).

Aaron Digulla
even those that are in the ORDER BY clause? I don't have those two in my GROUP BY.
Theresa
No, you do not need to put them in your order by clause
Xaisoft
I tried adding the two columns in the ORDER BY to the GROUP BY. That worked. Thanks!
Theresa
It doesn't make any difference in this particular case, but you have misunderstood Aaron and Xaisoft: generally speaking, you need to put all columns of the *SELECT* (not ORDER BY) in the GROUP BY clause. The point is that if you select some column and group the result along other columns, the value returned for the former column would have no meaning – it would be arbitrarily chosen by the RDBMS. You can only forgo putting SELECT columns into the GROUP BY clause if you compute some aggregate function over them, as Aaron said (MIN, MAX, SUM, etc.)
Arthur Reutenauer
Or to put it another way: If you have two columns and group by the first, that means you'll have several values from the second column per result row. Since there is only a single result row but many values to choose from, which one should the DB return? The first it stumbles upon?
Aaron Digulla