#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    129
    Rep Power
    16

    Difficult query with SUM and then > on SUM's total


    Here is the query I have so far:

    SELECT SUM(views) AS totalviews FROM topic t, topic_view v WHERE t.topic_id = v.topic_id AND status = 'processed' GROUP BY t.topic_id ORDER BY totalviews ASC

    Now within this query I'd like to get only records where TOTALVIEWS > 44 and I would rather not do this in PHP but in the SQL. Is this possible? I have tried:

    SELECT SUM(views) AS totalviews FROM topic t, topic_view v WHERE t.topic_id = v.topic_id AND status = 'processed' AND totalviews>44 GROUP BY t.topic_id ORDER BY totalviews ASC

    But I get an error.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    Paris Uppland
    Posts
    2,402
    Rep Power
    396
    Code:
     SELECT t.topic_id,SUM(views) AS totalviews 
    FROM topic t, topic_view v 
    WHERE t.topic_id = v.topic_id 
    AND status = 'processed' 
     GROUP BY t.topic_id 
    having sum(views) > 44
    ORDER BY totalviews ASC

IMN logo majestic logo threadwatch logo seochat tools logo