Solved

SQL Server select distinct

Posted on 2013-12-10
6
654 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 83

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 500 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 10

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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