有如上两个表,以左表为主,如何实现将另一个表中不同orderkey,相同item 的数据抓出来; 这个需求涉及两个不同数据集的比较,按常规的办法一般是需要通过可编程性的遍历来完成。其实也可以通过一条查询语句来完成:
select * from
(select orderkey,(select item from table1 as b where a.orderkey=b.orderkey order by item for xml path('') ) as item from table1 as a ) s1 left join
(select orderkey,(select item from table2 as b where a.orderkey=b.orderkey order by item for xml path('') ) as item from table2 as a ) s2 on s1.item=s2.item