Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to find distinct record by using join query in sql server 2008 r2 but getting an error "Column 'tbl_SalaryInformation.SalaryId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
"

And below its my query so please help me and give me better solution for that

SQL
select distinct si.Date, si.SalaryId,er.EmpCode, er.FName from tbl_EmpRegistration er,tbl_SalaryInformation si where er.EmpCode=si.EmpCode and si.PartnerName='Pio' and er.PartnerName='Pio' group by si.Date
Posted
Comments
Dinesh.V.Kumar 22-Sep-14 5:31am    
In order to group by you need to have a aggregate function in the select part of the query.
for eg:
Select si.Date, si.SalaryId,er.EmpCode, er.FName, sum(si.salary) from bl_EmpRegistration er,tbl_SalaryInformation si where er.EmpCode=si.EmpCode and si.PartnerName='Pio' and er.PartnerName='Pio' group by si.Date, si.SalaryId,er.EmpCode, er.FName

1 solution

It seems that you need to improve your SQL skill, Please read google SQL and group by clauses.

Apart from that you can have two solutions.

1. Remove your Group by Clause at all Like this

SQL
select distinct si.Date, si.SalaryId,er.EmpCode, er.FName from tbl_EmpRegistration er,tbl_SalaryInformation si where er.EmpCode=si.EmpCode and si.PartnerName='Pio' and er.PartnerName='Pio';

2. USe Group by with modifications in select clause

SQL
SELECT T.Date, T.MaxSalaryID SalaryId, er.EmpCode, er.FName
FROM
(
SELECT si.Date, si.EmpCode, MAX(si.SalaryId) MAXSalaryID
FROM tbl_SalaryInformation si
Group by si.Date, si.EmpCode
) T INNER JOIN tbl_EmpRegistration er on T.EmpCode = er.EmpCode
 
Share this answer
 
Comments
Dhananjay32 22-Sep-14 6:01am    
Thank you for giving reply and thank u for advice also

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900