group by and having with concatinated field [message #371235] |
Thu, 21 September 2000 16:53 |
Don Keyes
Messages: 3 Registered: September 2000
|
Junior Member |
|
|
We have a number of table with fiscal year and fiscal month fields. In Sybase we are able to find the latest records with a statement like:
select a,b,c
from x
group by a,b,c
having (fisc_yr || fisc_mth) = max(fisc_yr || fisc_mth)
When I try to run that in Oracle it gives me the error:
Oracle8i Enterprise Edition Release 8.1.6.0.0 - Production
With the Partitioning option
JServer Release 8.1.6.0.0 - Production
having (fisc_yr||fisc_mth) = max(fisc_yr || fisc_mth)
*
ERROR at line 12:
ORA-00979: not a GROUP BY expression
I tried adding the fisc_yr and fisc_mth fields to the select but it didn't help.
Is there any way to using the "having" clause on a combination of fields?
Thanks
|
|
|
|
Re: group by and having with concatinated field - found it! [message #371250 is a reply to message #371238] |
Fri, 22 September 2000 12:35 |
Don Keyes
Messages: 3 Registered: September 2000
|
Junior Member |
|
|
Hi,
Thanks for the suggestion. I had tried something similar but it only gave me the records that matched the maximum year and month
combination for the whole table. I need the maximum year-month combination for each value of field a.
I modified your suggestion and came up with this code that worked.
field a is the unique key field
select a,b,c
from tablex x1
where (x1.fisc_yr || x1.fisc_mth)=
(select max(x2.fisc_yr || x2.fisc_mth) from tablex x2
where x1.a = x2.a
group by x2.a)
group by x1.a,
x1.b,
x1.c
Joining the second table back to the first table on the key field a did the trick.
Thanks for pointing me in the right direction.
|
|
|
|