Solved

SQL Server select distinct

Posted on 2013-12-10
6
652 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
Backup Your Microsoft Windows Server®

Backup 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.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Detach & Attach 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 give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now