• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

sql SUM of a value error

Hi,
Im trying to get the total of a field in a database using the sql - SUM(Timespent) as Totaltimespent, Timespent is the name of the column with the numbers i want to add up.
My code below doesnt work
I'm using MS SQL
Thanks
SELECT tblClient.[name] as ClientName  ,  tblProject.[Name] as ProjectName  , tblEmployee.[Name] as EmployeeName , Date ,Timespent ,SUM(Timespent) as Totaltimespent, Task ,EmployeeID,tblTimesheet.ID as ID FROM tblTimesheet INNER JOIN tblProject on tblTimesheet.ProjectID = tblProject.ID INNER JOIN tblEmployee  on tblTimesheet.EmployeeID = tblEmployee.ID INNER JOIN tblClient on tblProject.ClientID = tblClient.ID
WHERE ProjectID=@ProjectID AND tblProject.ClientID =@ClientID

Open in new window

0
CharlieDev
Asked:
CharlieDev
  • 3
  • 3
  • 2
  • +1
4 Solutions
 
mirzasCommented:
You need to use a GROUP BY
0
 
mirzasCommented:
0
 
CharlieDevAuthor Commented:
Where do I put that?
Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
MilleniumaireCommented:
Try the following:
SELECT tblClient.[name] as ClientName,
       tblProject.[Name] as ProjectName,
       tblEmployee.[Name] as EmployeeName,
       tblTimesheet.ID as ID,
       SUM(Timespent) as Totaltimespent
FROM tblTimesheet 
     INNER JOIN tblProject 
        on tblTimesheet.ProjectID = tblProject.ID 
     INNER JOIN tblEmployee  
        on tblTimesheet.EmployeeID = tblEmployee.ID 
     INNER JOIN tblClient 
        on tblProject.ClientID = tblClient.ID
WHERE ProjectID=@ProjectID 
AND   tblProject.ClientID =@ClientID
GROUP BY tblClient.[name],
         tblProject.[Name],
         tblEmployee.[Name],
         tblTimesheet.ID 

Open in new window

0
 
quincydudeCommented:
mirzas:, we do not necessarily use group by for getting sum
CharlieDev, please provide more detail like table structure and what you are querying.
0
 
MilleniumaireCommented:
You need to use a group by statement if you are retrieving columns that are not aggregated.
0
 
mirzasCommented:
quincydude, it is one of the options available for the given criteria - it gives context to he aggregate functions.
0
 
CharlieDevAuthor Commented:
thanks for all the advice i have been advised that this method is not going to work for what i want so i'm now using asp.net code to add up the totals
Thanks
0
 
CharlieDevAuthor Commented:
thanks
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now