How do I calculate something using values from two tables?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abehm
    New Member
    • May 2007
    • 35

    How do I calculate something using values from two tables?

    SELECT (SUM(Hours)/EstimatedHours) AS Progress
    FROM Production.Time Log, Config.ProjectC onfig
    WHERE Production.Time Log.ProjectConf igID = 1

    I'm trying to calculate the progress of a task by adding all the hours from the timelog records and dividing it by the estimated time stored in the config table?
    I wrote the procedure above but I keep getting the following error:
    'Column 'Config.Project Config.Estimate dHours' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.'

    what am I missing?
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    try this

    Code:
    SELECT SUM(Hours/EstimatedHours) AS Progress
    FROM Production.TimeLog, Config.ProjectConfig
    WHERE Production.TimeLog.ProjectConfigID = 1
    the error you was getting was because sum was operating on hours alone and estimatedhours was outside of any aggregate function.
    When using aggregate queries, every field mentioned in the SELECTion must be aggregated in some way.

    Comment

    • abehm
      New Member
      • May 2007
      • 35

      #3
      i tried that and I still get progress = 0 for some reason

      for projectconfigid = 1
      if the timelog.hours records have 5, 12 and 10, and the projectconfig.e stimatedhours is 36, the progress value should read .75

      Comment

      • abehm
        New Member
        • May 2007
        • 35

        #4
        So I created the following view to get a total sum of all the hours and the projectconfigid .

        SELECT ProjectConfigID , SUM(Hours) AS TotalHours
        FROM Production.Time Log
        GROUP BY ProjectConfigID

        then I rewrote the procedure like this...

        SELECT CONVERT(numeric (8,2),(CONVERT( float,th.TotalH ours)/CONVERT(float,p c.EstimatedHour s))) AS Progress
        FROM vwTotalHours th INNER JOIN ProjectConfig pc ON th.ProjectConfi gID = pc.ProjectConfi gID
        WHERE pc.ProjectConfi gID = 1

        I kept getting 0's because i didn't realize if you divide two integers the answer will be in integer form.
        anyways, this is kinda messy but it seems to work now. Thanks for the help

        Comment

        Working...