SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Divide Queries

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Divide Queries

    Hello


    I have this querys:

    Code:
    SELECT genre
    FROM movies
    WHERE genre="Animation"
    
    SELECT COUNT(genre) AS Total 
    FROM movies
    WHERE genre="Animation";
    
    SELECT SUM(rating) AS SUMX
    FROM movies
    WHERE (genre="Animation");

    The querys are working well separately, but now i want the result= SUMX/Total


    Genre |SUMX |TOTAL |RESULT
    Animation 6 2 3

    Anyone knows help me?

    Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved.

    Code:
    SELECT genre, SUM (rating) AS TOTAL_SUM, COUNT(genre) AS TOTAL_GENRE, SUM(rating)/COUNT(genre) AS Total
    FROM movies
    WHERE Genre="Animation"
    GROUP BY genre
    Thanks anw

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    you can replace COUNT(genre) with COUNT(*) since genre will never be NULL in this query

    you can also replace SUM(rating)/COUNT(genre) with AVG(rating)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •