指定了非布尔类型的表达式... SQL Server

时间:2018-10-22 10:34:55

标签: sql sql-server subquery sql-in

select sp_name, office, comm 
from sperson
where (office,comm) in (select office, min(comm) from sperson group by office);

这适用于Oracle和MySQL。

1 个答案:

答案 0 :(得分:1)

您需要具有 correlation 的子查询,并且SQL Server将支持语法:

select sp.*
from sperson sp
where comm = (select min(sp1.comm)
              from sperson sp1
              where sp1.office = sp.office
             );

但是,这也可以通过row_number() / dense_rank()函数来实现:

select top (1) with ties sp.*
from sperson sp
order by row_number() over (partition by office order by comm);