我有两张桌子,我想加入:
表1
Room No Guest Nights Rate Gross Rent
2 John 11 120 1320
2 Michael 4 120 480
2 Helen 1 120 120
2 Andrew 7 120 840
表2
Room No Amount
1 1000
2 965
3 400
我想从表2到表1中加入数据并获得下表:
Room No Guest Nights Rate Gross Rent Amount
2 John 11 120 1320 965
2 Michael 4 120 480 Null
2 Helen 1 120 120 Null
2 Andrew 7 120 840 Null
当我Table 1 LEFT JOIN Table 2 ON Table 1.Room No=Table 2.Room No
时,金额为" 965"出现在每一行(((但我希望它只出现一次。
答案 0 :(得分:0)
要进行此类查询,您可以尝试将分析功能(例如LAG)设为https://oracle-base.com/articles/misc/lag-lead-analytic-functions 以下查询可以帮助您获得此类结果。
select Room_No ,
Guest ,
Nights ,
Rate ,
Gross_Rent ,
(CASE WHEN amt_prev<> Amount OR amt_prev is NULL THEN Amount
ELSE NULL
END) as amt
from (select 1.Room_No ,
1.Guest ,
1.Nights ,
1.Rate ,
1.Gross_ Rent ,
2.Amount ,
LAG(2.Amount, 1,NULL) OVER (ORDER BY 2.Amount) AS amt_prev
from Table1 1,Table2 2
where 1.Room_No=2.Room_No)
希望这会对你有帮助..