Some SQL trouble

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Beacher

    #1

    Some SQL trouble

    Hello all.
    I'm creating a view, and part of it.. one of the fields, i sum lets say
    hours + line hours As TotalHours, itll find the 3 records, sum them,
    and give me the correct amount of hours.. but I also have another field
    that is totalhours / product made .. I need this to also show in the
    view, but i first have to sum the total hours field. I tried doing
    (SELECT Sum(hours+lineh ours) from table) / productmade but it gives me
    the error about using an aggregate function in a group by or sum list?
    Anyways, I'm not too great in SQL so if anyone has any ideas, id
    greatly appreciate it. I can paste the code if it will help at all, but
    its rather lengthy.

    TIA

  • Rich P

    #2
    Re: Some SQL trouble

    I assume productmade is a number. How do you derive the productmade
    number? Does it come from the same table as hours, linehours? How does
    productmade relate to hours, linehours?

    Rich

    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • lesperancer@natpro.com

      #3
      Re: Some SQL trouble

      what is your initial query GROUP ed BY ?
      ie SELECT SUM(hours + lineHours) as totalHours FROM table GROUP BY
      something

      you need the same GROUP BY in
      (SELECT Sum(hours+lineh ours) from table group by something) /
      productmade

      the SUM function is an aggregate function (like COUNT, FIRST, MIN, MAX,
      etc) which needs a GROUP BY clause

      you can also have
      SELECT SUM(hours + lineHours) as totalHours
      SUM(hours + lineHours) / productMade as foobar
      FROM table
      GROUP BY something

      Comment

      Working...