Add SUM and AVG together also SUM and COUNT together

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1
Reputation: lixid is an unknown quantity at this point 
Solved Threads: 0
lixid lixid is offline Offline
Newbie Poster

Add SUM and AVG together also SUM and COUNT together

 
0
  #1
Dec 23rd, 2008
I am having a problem with my sql select statement below. What i am trying to accomplish is add a weighted gpa average to the actual average that gets calculated after a vendor gets assigned a gpa per job. The sectioned off area is where the problem lies. it is coming up with an average that doesnt calculate properly is there another way i could write this section of code to where i could add the sum and the avg together then also add a sum and a count together to get a correct total average? It would be a great help if someone could point me in the right direction i have googled this for two days and havent found exactly what i need. Thanks in advance.

  1. SELECT Vendors_1.VendorID AS [Vendor ID], Vendors_1.Name,
  2. (SELECT TypeName
  3. FROM VendorTypes
  4. WHERE (Vendors_1.VendorTypeID = VendorTypeID)) AS VendorType1,
  5. (SELECT TypeName
  6. FROM VendorTypes AS VendorTypes_3
  7. WHERE (Vendors_1.VendorTypeID2 = VendorTypeID)) AS VendorType2,
  8. (SELECT TypeName
  9. FROM VendorTypes AS VendorTypes_2
  10. WHERE (Vendors_1.VendorTypeID3 = VendorTypeID)) AS VendorType3,
  11. (SELECT COUNT(*) AS Expr1
  12. FROM BidInvites AS BI1
  13. WHERE (VendorID = Vendors_1.VendorID) AND (Invited = 1)) AS [Bids Sent],
  14. (SELECT COUNT(*) AS Expr1
  15. FROM BidInvites AS BI1
  16. WHERE (VendorID = Vendors_1.VendorID) AND (Awarded = 1)) AS Awarded,
  17.  
  18.  
  19. (SELECT (SUM(Vendors.InitGPA * Vendors.InitJobs) + AVG(VendorGPA.GPA)) / (SUM(Vendors.InitJobs) + COUNT(VendorGPA.GPA)) AS ttl
  20. FROM Vendors INNER JOIN
  21. VendorGPA ON Vendors.VendorID = VendorGPA.VendorID WHERE (VendorGPA.VendorID = Vendors_1.VendorID)) AS GPA
  22.  
  23.  
  24. , Vendors_1.City, Vendors_1.State, Vendors_1.ZipCode,
  25. Vendors_1.Phone, Vendors_1.Fax, Vendors_1.Email, Vendors_1.Mobile
  26. FROM Vendors AS Vendors_1 INNER JOIN
  27. VendorTypes AS VendorTypes_1 ON Vendors_1.VendorTypeID = VendorTypes_1.VendorTypeID
  28. WHERE (Vendors_1.IsInactive <> 1) OR
  29. (Vendors_1.IsInactive IS NULL)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC