Solved

SQL Server select distinct

Posted on 2013-12-10
6
646 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 82

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

15 Experts available now in Live!

Get 1:1 Help Now