Solved

sql distrinct query

Posted on 2009-07-14
10
272 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article describes some very basic things about SQL Server filegroups.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

777 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