Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Sort columns by highest values for each row
Old 07-25-2011, 11:35 PM Sort columns by highest values for each row
Junior Talker

Posts: 2
Trades: 0
Hi there,

I currently have a table with census data that is designed as follows: each row represents a location and each column is a language that contains the number of people in that location that speak the language. There are about 20 languages.

It looks something like this.

ID | language1 | language2 | language3|...| language N|
---------------------------------------------
1 |12 | 45 | 20 |...| 30 |
---------------------------------------------
2 | 50 | 40 | 10 |...| 60 |
---------------------------------------------
3 | 20 | 10 | 40 |...| 30 |

What I want to do is find out the top 5 languages spoken at each location.

Is there a query that will do this? I envision the final query table to look like this:

ID | First | Second | Third | Fourth| Fifth|
---------------------------------------------
1 | language2 | language N | language 3 |...| language 1 |
---------------------------------------------
2 | language1 | language 2 | language N |...| language 3 |
---------------------------------------------
3 | language3 | language N | language 1 |...| language 2 |

Thanks for the help!
sketchedin is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-26-2011, 04:11 AM Re: Sort columns by highest values for each row
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
No chance in a single query

You can ORDER rows BY column value in a query but you cannot change the order of the columns by a value in a row in a query.

To accomplish what you want it would need to restructure the rather poor table schema. The data relationships mean that it needs three tables, one for locations, one for languages and a linking table.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-26-2011, 09:12 AM Re: Sort columns by highest values for each row
Junior Talker

Posts: 2
Trades: 0
Thanks chrishirst. Do you think a Pivot query could do this? I have seen examples that look like would achieve this solution, but I do not understand how to use it.
sketchedin is offline
Reply With Quote
View Public Profile
 
Old 07-26-2011, 11:41 AM Re: Sort columns by highest values for each row
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quite probably, assuming your DB server supports pivots, failing that it will have to be a stored procedure.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-27-2011, 08:08 PM Re: Sort columns by highest values for each row
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
It took me a while to get my head round pivots - the example that finally got it to click was on asktom (aren't they all) - see http://asktom.oracle.com/pls/asktom/...31263576751669 and look down page to "Followup November 15, 2006 - 6am Central time zone"
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to Sort columns by highest values for each row
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21637 seconds with 12 queries