Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to order by two fields?? 3

Status
Not open for further replies.

jofarrell

Programmer
Mar 21, 2001
178
US
If I have two columns in a table

COL 1 Col 2
3 d4
4 f3
4 f3
3 d2
4 s3
3 d2

I would like to order by col 1 ... and also col 2 so that I can loop through my recordset and weed out duplicates

I would like to end up with
3 d2
3 d4
4 f3
4 s3

(Confused yet? :))

If anyone can think of a way to do this I would appreciate it.

Joanne

 
Absolutely. The "order by" clause can take as many columns as you want. You can also use "group by" to weed out duplicates.

select col1, col2 from your_table
group by col1, col2
order by col1, col2
 
Ahhh syntaxically I typed it wrong when doing my "order by" then I started doubting that I could ... Thank you so much :):)

Joanne
 
Jo,

If you want to find duplicates, you could also do:

SELECT col1, col2, COUNT(*)
FROM your_table
GROUP BY col1, col2,
ORDER BY col1, col2
HAVING COUNT(*) > 1

That will only list duplicate rows.

Tim
 
Thank you so much, that really helped and cut down a call to a table for me :)

Thank you again..

Joanne
 
BTW .... This has made some of my other code soooooo much shorter .... I feel like a dummy for forgetting the "Group By".


THANK YOU!!! It really did help in more than one place.

Joanne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top