Help writing a specific query please!

Posted on 2007-08-01
Last Modified: 2013-12-07
I need help writing this query for Sql Server.  

The table appears as:  

GalleryImageID (primary key)
GalleryID (FK)
Hits (bigint)
Total Raiting (bigint)

So there will be many images listed here with the same GalleryID, each with how many hits that image recieved and it's total raiting score.

I need 2 different queries.   I want to know the overall top galleryid's based on hits.  So it will have to go through, find all of the images for a single galleryid and add them up, do this for the several hundred thousand galleries that exist and then return the ones that have the top 10 or 20 or whatever.

Any help?
Question by:stryjews
    LVL 142

    Accepted Solution

    select top 1 g.galleryid
    from yourtable g
    group by g.galleryid
    order by sum(g.[Total rating]) desc

    LVL 1

    Author Comment

    You rock!  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
    From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
    This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
    This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now