Solved

SQL Server select distinct

Posted on 2013-12-10
6
658 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

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

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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