Sql Server 中交集查询问题

Hyacinthus 2002-10-12 11:30:13
Oracle 中的intersect (交集) 在Sql Server 中用什么代替?

比如
select max(fa_order) ma,fl_id from t_Flowaudit group by fl_id
得到结果为
ma fl_id
3 15
3 42
2 48
1 49
4 51
五条记录

select fa_order ma, fl_id from t_Flowaudit where us_id=98
得到结果为
ma fl_id
2 42
2 48
4 51
三条记录

我要的是在所得的两个数据集中都存在的记录(交集), 即:
要得到结果应该为
ma fl_id
2 48
4 51
两条记录

在Oracle 中可以如下得到
select fa_order ma, fl_id from t_Flowaudit where us_id=98
intersect
select max(fa_order) ma,fl_id from t_Flowaudit group by fl_id

在Sql Server中如何实现同样的功能????
用 EXISTS 查询出来的结果是不对的,
...全文
154 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hyacinthus 2002-10-12
  • 打赏
  • 举报
回复
呵呵, 谢谢各位!!
Anstinus 2002-10-12
  • 打赏
  • 举报
回复
这样不行吗
select fa_order ma, fl_id from t_Flowaudit where us_id=98 and exists (
select max(fa_order) ma,fl_id from t_Flowaudit t1 where t1.ma = t_Flowaudit.ma and t1.fl_id = t_Flowaudit.flid group by fl_id )
CSDNM 2002-10-12
  • 打赏
  • 举报
回复
exists:

select * from
(
select fa_order ma, fl_id from t_Flowaudit where us_id=98
) as a
where exists (
select 1 from
(
select max(fa_order) ma,fl_id from t_Flowaudit group by fl_id
) as b
where a.ma=b.ma and a.fl_id=b.fl_id
)

蓝天 2002-10-12
  • 打赏
  • 举报
回复
select a.ma,a.f1_id from (select max(fa_order) ma,fl_id from t_Flowaudit group by fl_id ) a ,t_Flowaudit b where a.ma=b.fa_order and a.fl_id =b.fl_id and b.us_id=98


CSDNM 2002-10-12
  • 打赏
  • 举报
回复
select a.* from
(
select fa_order ma, fl_id from t_Flowaudit where us_id=98
) as a,
(
select max(fa_order) ma,fl_id from t_Flowaudit group by fl_id
) as b
where a.ma=b.ma and a.fl_id=b.fl_id

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧