Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 results order by state with most rows

Author  Topic 

spikeypaul
Starting Member

2 Posts

Posted - 2006-12-08 : 16:27:44
sorry am new to sql and am stuck at what probably is a simple problem but at this time am lost. I have a table with 2 columns. 1st column contains name of a state(USA), 2nd column is of type bit.
I have 250 rows and i want to order them by state who has the most rows. For example.

column1 column2
______________________

Georgia 1
Georgia 1
Georgia 1
Texas 1
Texas 1
Florida 1

how can i achieve this?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-08 : 16:59:45
select column1, count(*)
from yourtablenamehere
group by column1
order by count(*) desc, column1

Didn't your professor show you this?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-09 : 02:58:30

Also Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

spikeypaul
Starting Member

2 Posts

Posted - 2006-12-11 : 15:39:20
The result will be something like this:

Georgia 3
Texas 2
Florida 1

This is what I initially got when i did the count query. What they want is have that same order (state with most rows) but to display all columns. So, once we figure out that Georgia has the most, then comes texas and florida; show them in that order but all detailed columns.

The result should look like

State......y/n.......Country......Zipcode
___________________________________________

Georgia Y USA 22934
Georgia Y USA 39489
Georgia Y USA 34452
Texas Y USA 11321
Texas Y USA 13215
Florida Y USA 33131
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-11 : 16:23:50
[code]select o.*
from yourinformationtablenamehere o
inner join (
select column1,
count(*) cnt
from yourtablenamehere
group by column1
) u on u.column1 = o.column1
order by u.cnt desc,
u.column1[/code]
Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -