SQL Query基于3个不同的表执行INNER JOIN

时间:2014-07-07 19:28:58

标签: sql sql-server-2008

最初,让我描述一下我希望我的查询返回的内容。我希望此查询返回Address表中具有AddressTypeLookupID为3或5的所有记录,这些记录在AddressType表中设置。其次,作为进一步的限制,这还必须仅包括IndustryTypeID = 56的公司,该公司在IndustryType表中设置。 Address,AddressType和IndustryType表中的所有记录都按ID链接。

SELECT B.AddressTypeLookupID, A.Address1, A.Address2, A.City, A.State, A.Zip, A.ID
FROM Company.dbo.Address A 
INNER JOIN Company.dbo.AddressType B ON A.AddressID = B.AddressID
WHERE B.Status = 'Active' AND
      B.AddressTypeLookupID = 3 AND
      A.AddressID = (SELECT MAX(Z.AddressID) 
                    FROM Company.dbo.Address Z 
                      INNER JOIN Company.dbo.AddressType X ON Z.AddressID = X.AddressID
                    WHERE B.Status = 'Active' 
                      AND B.AddressTypeLookupID = 3 
                      AND Z.ID = A.ID
                    )
INNER JOIN COMPANY.dbo.IndustryTypeCompanyName g ON g.ID = A.ID
WHERE g.IndustryTypeID = 56

UNION ALL

SELECT B.AddressTypeLookupID, A.Address1, A.Address2, A.City, A.State, A.Zip, A.ID
FROM Company.dbo.Address A 
INNER JOIN Company.dbo.AddressType B ON A.AddressID = B.AddressID
WHERE B.Status = 'Active' AND
      B.AddressTypeLookupID = 5  AND
      A.AddressID = (SELECT MAX(Z.AddressID)
                  FROM Company.dbo.Address Z 
                  INNER JOIN Company.dbo.AddressType X ON Z.AddressID = X.AddressID
                  WHERE B.Status = 'Active' AND
                        B.AddressTypeLookupID = 5 AND
                        Z.ID = A.ID                        
                  )
INNER JOIN COMPANY.dbo.IndustryTypeCompanyName j ON j.ID = A.ID
WHERE j.IndustryTypeID = 56

我相信,我的问题在于上面称为“g”和“j”的内部联接,我试图通过IndustryTypeID = 56来限制查询。查询在没有这些行的情况下正常工作(根据需要)。我只是将IndustryTypeID的(完整)查询限制为56.我在逻辑上错了,我或者我根本不理解SQL语法。我正在使用Microsoft SQL Management Studio 2008.

3 个答案:

答案 0 :(得分:1)

我不确定最后一次加入应该是什么意思,但据我所知,您的查询可以简化为:

SELECT B.AddressTypeLookupID, A.Address1, A.Address2, A.City, A.State, A.Zip, A.ID
FROM Company.dbo.Address A 
JOIN Company.dbo.AddressType B 
    ON A.AddressID = B.AddressID
JOIN COMPANY.dbo.IndustryTypeCompanyName g 
    ON g.ID = A.ID
WHERE B.Status = 'Active' 
  AND B.AddressTypeLookupID in (3,5) 
  AND A.AddressID = ( SELECT MAX(Z.AddressID) 
                      FROM Company.dbo.Address Z 
                      JOIN Company.dbo.AddressType X 
                          ON Z.AddressID = X.AddressID
                      WHERE 
                      -- redundant predicates removed
                      -- B.Status = 'Active' 
                      -- AND B.AddressTypeLookupID = 3 
                      -- AND 
                             Z.ID = A.ID
                    )          
  AND g.IndustryTypeID = 56

答案 1 :(得分:1)

我现在看到问题,我们知道这是一个语法错误。你有一个内连接,一个在哪里,然后是另一个内连接,然后是另一个。每个组只能按要求的顺序出现一次,但您可以多次使用关键字加入。所以选择,从,加入,加入哪里。

SELECT B.AddressTypeLookupID, A.Address1, A.Address2, A.City, A.State, A.Zip, A.ID
FROM Company.dbo.Address A
INNER JOIN Company.dbo.AddressType B
ON A.AddressID = B.AddressID
INNER JOIN COMPANY.dbo.IndustryTypeCompanyName g
ON g.ID                   = A.ID
WHERE B.Status            = 'Active'
AND B.AddressTypeLookupID = 3
AND A.AddressID           =
  (SELECT MAX(Z.AddressID)
  FROM Company.dbo.Address Z
  INNER JOIN Company.dbo.AddressType X
  ON Z.AddressID            = X.AddressID
  WHERE B.Status            = 'Active'
  AND B.AddressTypeLookupID = 3
  AND Z.ID                  = A.ID
  )
AND g.IndustryTypeID = 56

答案 2 :(得分:1)

由于你已经在你的选择中包含了地址表而不是选择max,因此更容易使用ROW_NUMBER()来解决每组1个问题

With cte as
(
    SELECT B.addresstypelookupid, 
           A.address1, 
           A.address2, 
           A.city, 
           A.state, 
           A.zip, 
           A.id ,
          ROW_NUMBER() OVER  (Partition BY b.addresstypelookupid  
                               order by addressid desc  )  rn
    FROM   company.dbo.address A 
           INNER JOIN company.dbo.addresstype B 
                   ON A.addressid = B.addressid 
                      AND B.addresstypelookupid IN ( 3, 5 ) 
                      AND B.status = 'Active' 
           INNER JOIN company.dbo.industrytypecompanyname g 
                   ON g.id = A.id 
    WHERE  g.industrytypeid = 56 
)
SELECT * FROM CTE WHERE Rn = 1