?
Solved

sql distrinct query

Posted on 2009-07-14
10
Medium Priority
?
294 Views
Last Modified: 2012-05-07
hi all,

can someone tell me how i can do this:

lets say i have a table: (idcol being a primary key)

idcol col1
1       200
2       200
3       300
4       400
5       900
6       3000

i want a query that will distinct just the second field (col1) column so return will look like:

Id     col1
1      200
2      300
3      400
4      900
5      3000

the id in the return table DOES NOT need to be value pulled from the original idcol (assuming it probably cant without being part of an aggregate function anyways). im happy if we build a id  from say ROW_NUMBER

someone help please.....
0
Comment
Question by:theoaks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24855763
so, something like this?

select col1, newidcol = row_number() order by idcol)
from tablename
group by col1
0
 
LVL 5

Author Comment

by:theoaks
ID: 24855812
threw an error expectiong "over" so i did:

select col1, newidcol = row_number() over(order by idcol)
from tablename
group by col1

and now saying that idcol is not contained in an aggregate function
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1600 total points
ID: 24855825
Just add an aggregate like min.
select col1, newidcol = row_number() over(order by min(idcol))
from tablename
group by col1

Open in new window

0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24855846
This also works if the newid corresponds to the lowest to highest values in col1.
select col1, newidcol = row_number() over(order by col1)
from t
group by col1

Open in new window

0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 400 total points
ID: 24855942
You can also try with dense_rank and distinct
select distinct col1,dense_rank() over(order by col1) as newidcol from Yourtable

Open in new window

0
 
LVL 5

Author Comment

by:theoaks
ID: 24856034
thanks guys both worked well!!

much appreciated
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24856055
well poop....I missed out.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24856085
@chapmandew, thought you would have been credited for original suggestion.  I was just helping get over the syntax issues.  :) Sorry about that.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24856093
No worries at all....Im just getting back into the swing of things...have been out for some time
0
 
LVL 5

Author Comment

by:theoaks
ID: 24856658
sorry chapmandew, but my original efforts where already pretty close to correct, and i need an exact answer as i wasnt that far off myself ( i just missed the aggregate function in the over clause)...

thanks anyway
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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