?
Solved

SQL Server select distinct

Posted on 2013-12-10
6
Medium Priority
?
680 Views
Last Modified: 2013-12-11
In SQL Server, how do you select a distinct with multiple columns?  I want distinct employeeid, but it's not the first column in my select, so it doesn't seem to work.

select count(*) as seqnumber, Recordtype, CoID, EmployeeID, EventCode, CreationDate
I want to select distinct employeeids, that is, if an employeeid is in there more than once, I only want it once.

Thanks.
0
Comment
Question by:newtoperlpgm
6 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39710272
So why don't you make it the first column and see what you get?
select DISTINCT EmployeeID, count(*) as seqnumber, Recordtype, CoID, EventCode, CreationDate

Open in new window

0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39710279
I always tend to think GROUP BY is a better more explicit option. It forces you to identify the logic of how to pick the other values.

This is a nice clear example:
http://blog.sqlauthority.com/2007/03/29/sql-server-difference-between-distinct-and-group-by-distinct-vs-group-by/
0
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 2000 total points
ID: 39710294
If you want to pick just the first row of a group (based on a specific sort) you can use ROW_NUMBER().
SELECT rn = ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY EventCode DESC), 
    Recordtype, 
    CoID, 
    EmployeeID, 
    EventCode, 
    CreationDate 
FROM your_table 
WHERE rn = 1

Open in new window

...but you'd have to do a join to include the count as well.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 11

Expert Comment

by:HuaMinChen
ID: 39710720
You need to use "group by" the columns including employeeid. Read:
http://www.w3schools.com/sql/sql_groupby.asp
http://technet.microsoft.com/en-us/library/ms173245.aspx
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39711069
@newtoperlpgm - In the case that there are multiple records for a single EmployeeID, can you please describe the logic for which record you would like returned? e.g. would you like the one with the most recent creation date?

It's a little unusual to have a query which returns columns related to a single record and a count which relates to a group of records. It's perfectly possible to combine both but we need a fuller description of the intended logic.
0
 

Author Comment

by:newtoperlpgm
ID: 39712944
Thanks for all your help.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question