Oracle SQL Tips

左连接的同时只输出关联表的一条记录

WITH X AS

(SELECT
1
ID
FROM DUAL UNION
SELECT
2
FROM DUAL UNION
SELECT
3
FROM DUAL),

Y AS

(SELECT
1
ID, 1 NR, 'B'
CODE

FROM DUAL

UNION

SELECT
1, 2, 'A'

FROM DUAL

UNION

SELECT
2, 2, 'A'

FROM DUAL) -- end of test data

SELECT *

FROM (SELECT X.ID,

Y.NR,

Y.CODE,

ROW_NUMBER() OVER(PARTITION
BY X.ID ORDER
BY Y.NR) AS RN

FROM X

LEFT
OUTER
JOIN Y

ON Y.ID = X.ID)

结果如下:

   
上一篇:[.NET领域驱动设计实战系列]专题五:网上书店规约模式、工作单元模式的引入以及购物车的实现


下一篇:如何烧写BIOS到SD卡里面