1

相关表:
DepartmentPhone:DepartmentPhoneID int、DepartmentID int、PhoneID int
Phone:PhoneID int、PhoneType int

有 6 个 PhoneType=4 的电话属于 DepartmentID=2。所以这会产生 6 条记录:

select *
from DepartmentPhone
  join Phone on Phone.PhoneID = DepartmentPhone.PhoneID and Phone.PhoneType = 4
where DepartmentPhone.DepartmentID = 2

请注意,DepartmentID=2 用于说明目的,我的查询将包含所有部门。
我想要实现的是为每个部门选择第一个电话(类型 = 4) - 每个部门只有 1 行。我认为以下查询可以解决问题,但它会不断检索所有 6 条记录。我错过了什么?

select x.*
from DepartmentPhone x
where 
  x.DepartmentID = 2
  and x.PhoneID = (select max(y.PhoneID)
                   from departmentphone y 
                     join Phone on y.PhoneID = Phone.PhoneID and Phone.PhoneType = 4
                   where x.DepartmentPhoneID = y.DepartmentPhoneID)

谢谢你的帮助!!!

4

2 回答 2

2

我希望有一个干净的语法。最好是使用ROW_NUMBER

;WITH DepartmentPhone_CTE AS
(
    SELECT p.*, 
        ROW_NUMBER() OVER
            (PARTITION BY dp.DepartmentID ORDER BY dp.PhoneID) AS RowNum
    FROM DepartmentPhone dp
    INNER JOIN Phone p
        ON p.PhoneID = dp.PhoneID
    WHERE p.PhoneType = 4
)
SELECT dp.*
FROM DepartmentPhone_CTE
WHERE RowNum = 1
于 2010-01-13T01:34:37.700 回答
0

我不像你那样了解你的架构,但我猜你需要用 来关联你的分组DepartmentID,而不是DepartmentPhoneID.

select x.*
from DepartmentPhone x
where 
  x.DepartmentID = 2
  and x.PhoneID = (select max(y.PhoneID)
                   from departmentphone y 
                     join Phone on y.PhoneID = Phone.PhoneID and Phone.PhoneType = 4
                   where x.DepartmentID = y.DepartmentID);

这里有几个替代查询应该在不使用相关子查询的情况下获得相同的结果。第一个使用派生表:

select *
from DepartmentPhone x
join (select d.DepartmentID, max(d.PhoneID) as maxPhoneID
      from DpartmentPhone d join Phone p using (PhoneID)
      where p.PhoneType = 4
      group by d.DepartmentID) y
  using (DepartmentID);

第二种选择根本不使用子查询,而是使用自连接:

select d1.*
from DepartmentPhone d1
join Phone p1 on d1.PhoneID = p1.PhoneID and p1.PhoneType = 4
left outer join (DepartmentPhone d2 join Phone p2 
    on d2.PhoneID = p2.PhoneID and p2.PhoneType = 4)
  on d1.DepartmentID = d2.DepartmentID and d1.PhoneID < d2.PhoneID
where d2.DepartmentID is NULL;
于 2010-01-13T01:33:13.277 回答