INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

TOP 5 selection

TOP 5 selection

(OP)
I have table with ID & STATES columns.
    STATES          ID
     FL             8
     CA             9
     MN             7
     TX             9
     DC             12
     IO             2
     AR             5
I want the TOP 5 Id's GROUP BY STATES & ORDER BY ID DESC
 & then the TOTAL of TOP 5 id's only not all id's.
    

RE: TOP 5 selection

try:

SELECT TOP 5 States, ID
FROM mytable
GROUP BY States
ORDER BY ID Desc

-DNG

RE: TOP 5 selection

(OP)
This is what I'm getting ,now I want to calculate the sum(ID's) only for top 5 STATES which is:
 DC             12
 CA             9
 TX             9
 FL             8
 MN             7
   TOTAL       ----
                55

RE: TOP 5 selection

Try

SELECT TOP 5 States, ID, SUM(ID) as TotalSum
FROM mytable
GROUP BY States
ORDER BY ID Desc

-DNG

RE: TOP 5 selection

(OP)
I have to include ID in GROUP BY clause also(otherwise its giving me the error). & when I run this query as
SELECT TOP 5 States, ID, SUM(ID) as TotalSum
FROM mytable
GROUP BY States,ID
ORDER BY ID Desc

all no.s are changed.. I want the SUM of all TOP 5 States..
GROUP BY is giving me for all states..

RE: TOP 5 selection

How about this:

SELECT State, ID, SUM(ID) as TotalSum
FROM mytable WHERE ID IN (SELECT TOP 5 ID FROM
mytable ORDER BY ID DESC)
GROUP BY State, ID
ORDER BY ID DESC

-DNG

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close