Select date that a sum()'s threshold is met

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freeskier
    New Member
    • Oct 2006
    • 65

    Select date that a sum()'s threshold is met

    I need to be able to tell the date of when a sum is met or exceeded. For example:
    Code:
    session(sessionid, personid, date, minutes)
    sessionid is PK, personid is FK, there will be multiple records for each personid
    I would like to know the date for when sum(minutes) >= 400 for each personid

    I've been trying something like this without success
    Code:
    SELECT min(a.dated), a.personid, thesum FROM (
    
    SELECT sum(t1.minutes) as thesum, t2.dated, t2.personid
    FROM session t1
    INNER JOIN session t2 ON t1.personid = t2.personid
    AND t1.dated <= t2.
    GROUP BY t2.dated, t2.personid
    HAVING thesum >= 400
    ) a 
    
    group by a.dated, a.personid
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    having sum(t1.minutes) >= 400

    it's kind of stupid that you aren't able to alias it.

    Comment

    • freeskier
      New Member
      • Oct 2006
      • 65

      #3
      Thank you for your fast reply. Your tip helped me. I had to make one other change:

      GROUP BY t2.sessionid, t2.personid

      GROUP BY a.personid

      because each person may have multiple entries for the same day.

      Thank you so much for your advice, this site has been very useful to me for several years

      Comment

      Working...