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

CharlieDevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mirzasCommented:
You need to use a GROUP BY
mirzasCommented:
CharlieDevAuthor Commented:
Where do I put that?
Thanks
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
MilleniumaireCommented:
You need to use a group by statement if you are retrieving columns that are not aggregated.
mirzasCommented:
quincydude, it is one of the options available for the given criteria - it gives context to he aggregate functions.
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
CharlieDevAuthor Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.