samedi 1 août 2015

DISTINCT in SQL Server on a single column

I am trying to run a SQL query in SQL Server 2005.

select distinct 
    emp_code, SSN, name
from 
    dbo.employee
where 
    (City = 'Abc') 
    or
    (Country = 'India') 
and Joining_date between getdate()-60 and getdate()

which is returning 76 rows.

But if instead I use

select distinct 
    emp_code
from 
    dbo.employee
where 
    (City = 'Abc') 
    or
    (Country = 'India') 
    and Joining_date between getdate()-60 and getdate()

it returns 73 records.

I understand that in first query SQL Server is returning where all 3 columns are distinct.

How should I write the query if I want only distinct emp_code ?

I have tried using group by emp_code but it is throwing an error

Please help!

Aucun commentaire:

Enregistrer un commentaire