Sql查询连接问题

时间:2015-05-13 11:16:07

标签: sql sql-server join

我正在努力进行SQL查询。我想把其他表中的总和包括在内。

    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 

我想在表tblTimeRecord中包含这个sum-block。

我使用此代码得到了一些时间补偿

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

猜猜我是在加入吗?

搞定了。

 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 

1 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点。

您可以使用WITH子句创建聚合表,然后将其连接到主查询。

或者这样做:

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         

我更喜欢第二种方式。