0

这已经解决了,我需要将 SQL 中的 4 个表与不同表中的 2 列连接为不同的。直到现在,我一直会得到重复的行。

我有 4 个表,我试图加入 1 个查询。

我需要创建一个如下所示的报告:

quoteid | dateEntered | insuredName | admin initials | quoteType | status | last note usertype

Select quoteID,insuredFirstName,insuredLastName,quoteType,status, firstname, lastname, adminInitial, userType
from (SELECT Row_Number() Over(Partition by A.quoteid  order by A.quoteid ) as Row , A.quoteID, A.insuredFirstName, A.insuredLastName, A.quoteType, A.status, B.firstName, B.lastName, left(C.firstName,1) + left(C.lastName,1) as adminInitial, D.userType
    FROM quotes A
    INNER JOIN tbl_agents B
    ON A.createUserID = B.AgentNumber
    INNER JOIN tbl_admins C
    ON A.assignedID = C.ID
    INNER JOIN
        (SELECT 
            quoteID, userType
            FROM quote_notes) D
            ON A.quoteID = D.quoteID) as t where row=1 
4

2 回答 2

2

尝试DISTINCTinner查询移动到outside查询。

于 2013-04-30T17:45:32.047 回答
1

尝试像这样使用这样的Row_Number()使用Partition by......

  Select quoteID,insuredFirstName,insuredLastName,quoteType,status from (SELECT Row_Number() Over(Partition by quoteid ,Name1  order by quoteid ) as Row , A.quoteID, A.insuredFirstName, A.insuredLastName, A.quoteType, A.status, B.firstName, B.lastName, left(C.firstName,1) + left(C.lastName,1) as adminInitial, D.userType
        FROM quotes A
        INNER JOIN tbl_agents B
        ON A.createUserID = B.AgentNumber
        INNER JOIN tbl_admins C
        ON A.assignedID = C.ID
        INNER JOIN
            (SELECT 
                quoteID, userType
                FROM quote_notes) D
                ON A.quoteID = D.quoteID) as t where row=1 
于 2013-05-10T16:41:00.530 回答