Hi,

I have a java appllication which has a query depending on the requirement,

select EmployeeID, TransactionAmount from employee where EmployeeID IN ('E123','E234') group by EmployeeID;

which works well in mysql, but for oracle a get an error like
[Err] ORA-00979: not a GROUP BY expression

Can anyone help me in this.?

Thanks

Recommended Answers

All 26 Replies

Try this.

select EmployeeID, sum(TransactionAmount) as TotalAmount from employee where EmployeeID IN ('E123','E234') group by EmployeeID;

I actually want to select the last transaction amount,

so my query looks like

select EmployeeID, TransactionAmount from employee where EmployeeID IN ('E123','E234') group by EmployeeID ORDER BY TransactionDate DESC;

You can't use GROUP BY clause without using any grouping function in Oracle.

You can't use GROUP BY clause without using any grouping function in Oracle.

Is there any other way i can achieve this.

why you need to use GROUP BY to select the last transaction amount ?

I want last transaction amount for every employee id. So i have group it by employeeID.

How you define last transaction amount ?

last transaction amount is the latest transaction amount an employee has transacted for an employee table.

So there will be only one last traction for any employee, right ?

There can be many transactions for a single employee.

But there can be only one last traction .

Yes, only one last transaction amount.

how is your structure

1) is there any possibility that an employee transacted more than once in same date
or
2)for one date employee will have only one transaction

select your case 1 or 2

Yes, only one last transaction amount.

Then why you need GROUP BY ?

I assume your employee table has datetime field named trans_date_time (you must change it to exact name at 2 place in following query).

select a.EmployeeID, TransactionAmount  from 
employee a left outer join (
select EmployeeID, max(trans_date_time)  as max_dt_time
 from employee where EmployeeID IN ('E123','E234') group by EmployeeID) b 
on a.employeeid=b.employeeid and a.trans_date_time=b.max_dt_time

If you do not have time component and if employees has more than one transaction in same date then this query will fail

commented: agree +13

if i dont use group by clause then i get many records for every employee.??

That is because you are not checking for last transaction in your code.

That is because you are not checking for last transaction in your code.

How will i be able to do it

but left outer join gives bad performance right

who told you ?

as per what i know... Is it not so.???

no.

Thanks for the help..

Try following Query using Analytical function.......

select employeeid,TransactionAmount,max_date  from
(
select EmployeeID, TransactionAmount,TransactionDate,
max(TransactionDate) over (partition by employeeid order by employeeid) max_date
from employee
)
where TransactionDate = max_date 
order by employeeid;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.