Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Need help with MS SQL Select Distinct Query - multiple columns

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
TrentSlater
Asked:
TrentSlater
  • 3
  • 3
  • 2
  • +2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
TrentSlaterAuthor Commented:
That works fine to remove the duplicates but it is sorting by ID, not column0. I need it sorted by column0
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
k_romychCommented:
SELECT Column0, ID FROM all_codes
GROUP by Column0
ORDER BY id
0
 
TrentSlaterAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
cyberkiwiCommented:
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
 
k_romychCommented:
SELECT Column0, ID FROM all_codes
GROUP by Column0
ORDER BY Column0
0
 
vsosuCommented:
Here you go:

SELECT Column0
FROM all_codes
GROUP by Column0
ORDER BY  min(val)
0
 
TrentSlaterAuthor Commented:
Thanks AngelIII - version 3 worked like a charm. Thanks to all for the quick response.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now