Solved

sql distrinct query

Posted on 2009-07-14
10
265 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
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.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

867 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

14 Experts available now in Live!

Get 1:1 Help Now