0

I am struggling with a sql query. I want to include the sum from an other table.

    SELECT DISTINCT 
    tblProject.CompanyID, 
    tblCompany.Name, 
    tblCompany.AvtalsKund, 
    tblProject.ProjectName, 
    tblProject.Estimate, 
    tblProject.ProjectStart, 
    tblProject.Deadline, 
    CONVERT(VARCHAR(8), tblProject.Deadline, 2) AS [YY.MM.DD] , 
    tblProject.PreOffered, 
    tblProject.ProjectType, 
    tblProjectType.ProjType, 
    tblOrdered.FirstName + + tblOrdered.LastName as OrderedFullName, 
    tblProject.ProjectID, 
    tblProject.RegDate, 
    tblProject.ProjectNr, 
    tblProject.ProjectNr 
    FROM tblProject 

    INNER JOIN tblCompany ON tblProject.CompanyID = tblCompany.CompanyID 
    ---> INNER JOIN (SELECT tblTimeRecord.ProjectID, SUM(CONVERT(float,replace([Hours],',','') )) 
FROM tblTimeRecord group by tblTimeRecord.ProjectID) as b
    ON b.ProjectID =  tblProject.ProjectID
    INNER JOIN tblTimeRecord ON tblTimeRecord.ProjectID = tblProject.ProjectID
    INNER JOIN tblProjectType ON tblProject.ProjectType = tblProjectType.ProjTypeID 
    LEFT OUTER JOIN tblOrdered ON tblProject.OrderedBy = tblOrdered.OrderedID 
    LEFT OUTER JOIN tblRel_WorkerProject ON tblProject.ProjectID = tblRel_WorkerProject.ProjectID 
    LEFT OUTER JOIN tblPerson ON tblPerson.PersonID = tblRel_WorkerProject.WorkerID 
    LEFT OUTER JOIN tblRel_StatusWorkerProject ON tblProject.ProjectID = tblRel_StatusWorkerProject.ProjectID 

I want to include this sum-block from table tblTimeRecord.

I get a sum of timerapports with this code

    SELECT tblTimeRecord.ProjectID, 
SUM(CONVERT(float,replace([Hours],',','') )) 
FROM tblTimeRecord where ProjectID=1312 group by tblTimeRecord.ProjectID 

Guess i do it in join?


Got it working with this.

 SELECT DISTINCT
tblProject.ProjectID,
Summa, 
tblProject.CompanyID, 
tblCompany.Name, 
tblCompany.AvtalsKund, 
tblProject.ProjectName, 
tblProject.Estimate, 
tblProject.ProjectStart, 
tblProject.Deadline, 
CONVERT(VARCHAR(8), tblProject.Deadline, 2) AS [YY.MM.DD] , 
tblProject.PreOffered, 
tblProject.ProjectType, 
tblProjectType.ProjType, 
tblOrdered.FirstName + + tblOrdered.LastName as OrderedFullName, 
tblProject.ProjectID, 
tblProject.RegDate, 
tblProject.ProjectNr, 
tblProject.ProjectNr 
FROM tblProject 
INNER JOIN tblCompany ON tblProject.CompanyID = tblCompany.CompanyID 
INNER JOIN (SELECT tblTimeRecord.ProjectID, SUM(CONVERT(float,replace([Hours],',','') )) as Summa FROM tblTimeRecord group by tblTimeRecord.ProjectID) as b
ON b.ProjectID =  tblProject.ProjectID
INNER JOIN tblTimeRecord ON tblTimeRecord.ProjectID = tblProject.ProjectID
INNER JOIN tblProjectType ON tblProject.ProjectType = tblProjectType.ProjTypeID 
LEFT OUTER JOIN tblOrdered ON tblProject.OrderedBy = tblOrdered.OrderedID 
LEFT OUTER JOIN tblRel_WorkerProject ON tblProject.ProjectID = tblRel_WorkerProject.ProjectID 
LEFT OUTER JOIN tblPerson ON tblPerson.PersonID = tblRel_WorkerProject.WorkerID 
LEFT OUTER JOIN tblRel_StatusWorkerProject ON tblProject.ProjectID = tblRel_StatusWorkerProject.ProjectID 
3
  • To use aggregate functions (e.g. SUM), you need a GROUP BY clause. Commented May 13, 2015 at 11:19
  • Yes but where and how Commented May 13, 2015 at 11:21
  • Either you add the GROUP BY to your huge SELECT, or use a correlated sub-query to count. Commented May 13, 2015 at 11:22

1 Answer 1

1

There are two ways to do this.

You can use a WITH clause to create the aggregate table then join this to the main query.

Or do it this way:

SELECT m.BLAH
       ,m.FOO
       ,x.AMOUNT
FROM   MAINTABLE m
     LEFT JOIN
             (
                SELECT FOO
                       ,SUM(AMOUNT) as AMOUNT
                FROM   OTHERTABLE
                GROUP BY FOO
             ) x
             ON m.FOO = x.FOO         

I prefer the second way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.