Solved

Need help with MS SQL Select Distinct Query - multiple columns

Posted on 2010-09-20
10
436 Views
Last Modified: 2012-05-10
I have the query below:

select distinct column0, id
from all_codes
group by id,column0

The ID field is an auto increment field and is necessary to keep the codes in cloumn0 in a specific order.

If I run this query it does not select unique records from Column0, but returns all records. (There are about 20,000 duplicates in column0 out of total 1 million records)

I need to return only unique records in column0 but keep the sort on the ID field.

Sample data shown below:
Column0 ID
PB-2760      1
LB-2662      2
YO-0215      3
JY-5620      4
BK-3187      5
WY-8750      6
KL-5024      7
FB-2647      8
JO-9972      9
TY-4709      10

I can use the query:

select distinct column0 from codes_all

but then SQL messes up the order of column0 - I need it in the order in which it was imported in to the table.

Using MS SQL 2005 Express.

Help needed ASAP

Thanks!
0
Comment
Question by:TrentSlater
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33715394
you want this:
select column0, id
 from (
  select column0, id, row_number() over (partition by column0 order by id) rn
    from all_codes
 ) sq
where rn = 1

Open in new window

0
 

Author Comment

by:TrentSlater
ID: 33715416
That works fine to remove the duplicates but it is sorting by ID, not column0. I need it sorted by column0
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33715419
this should do it:
select column0, id
 from (
  select column0, id, row_number() over (partition by column0 order by id) rn
    from all_codes
 ) sq
where rn = 1
order by column0

Open in new window

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 2

Expert Comment

by:k_romych
ID: 33715431
SELECT Column0, ID FROM all_codes
GROUP by Column0
ORDER BY id
0
 

Author Comment

by:TrentSlater
ID: 33715432
Sorry - still sorting by ID.

Could we copy these into a new table and then run a select * query???

I only need the ID field to sort them.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33715492
ok, this should do:
select column0, id
 from (
  select column0, id, row_number() over (partition by column0 order by id) rn
    from all_codes
 ) sq
where rn = 1
order by id

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33715493
a3's query.
But I think this is what you mean

select column0
 from (
  select column0, id, row_number() over (partition by column0 order by id) rn
    from all_codes
 ) sq
where rn = 1
order by id
0
 
LVL 2

Expert Comment

by:k_romych
ID: 33715541
SELECT Column0, ID FROM all_codes
GROUP by Column0
ORDER BY Column0
0
 
LVL 2

Expert Comment

by:vsosu
ID: 33715564
Here you go:

SELECT Column0
FROM all_codes
GROUP by Column0
ORDER BY  min(val)
0
 

Author Closing Comment

by:TrentSlater
ID: 33715640
Thanks AngelIII - version 3 worked like a charm. Thanks to all for the quick response.
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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

832 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