Solved

sql distrinct query

Posted on 2009-07-14
10
253 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
  • 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 59

Accepted Solution

by:
Kevin Cross earned 400 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
 
LVL 59

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 40

Assisted Solution

by:Sharath
Sharath earned 100 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
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.

 
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 59

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

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.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

18 Experts available now in Live!

Get 1:1 Help Now