[英]SQL - Joining one table with another based on ( Row value from first table to Column value of second table)
我有兩個表,如下所示
第一張桌子:
DealNum CurrencyValue CurrencyCode Date
110 100 AA 01/12/2011
111 200 AB 01/11/2011
112 120 AC 01/10/2011
113 20 AA 01/11/2011
110 103 AD 01/12/2011
115 200 AD 01/11/2011
119 120 AG 01/10/2011
130 20 AK 01/11/2011
第二表
CurrencyCode OCT NOV DEC JAN ..
AA 0.91 0.88 0.9 0.94
AB 0.9 0.8 0.96 0.89
AC 0.81 0.79 0.82 0.84
AD 0.4 0.41 0.42 0.39
AE 0.9 0.92 0.91 0.94
AF 0.8 0.82 0.83 0.81
現在我要在以下條件下將第二個表中的數據添加到新列中的第一個表中
1.It has to do based on the CurrencyCode and month
2.If the deal is from DEC and currencyCode is AA , then it has to take the value 0.9,
if the deal is from NOV and currencyCode is AA , then it has to take the value 0.88..
因此,結果應如下所示
DealNum CurrencyValue CurrencyCode Date NewColumn
110 100 AA 01/12/2011 0.9
111 200 AB 01/11/2011 0.8
112 120 AC 01/10/2011 0.81
113 20 AA 01/11/2011 0.88
110 103 AD 01/12/2011 0.42
115 200 AD 01/11/2011 0.41
119 120 AG 01/10/2011 --
130 20 AK 01/11/2011 --
我不知道如何比較第一張表中月份的行值和第二張表中月份的列值。 請在這件事上給予我幫助..
提前致謝。
干杯,哈里斯。
盡管您的第二張表設計得不好,但這應該可以工作。
SELECT DealNum
, CurrencyValue
, CurrencyCode
, Date
, Deal =
CASE MONTH(t1.Date)
WHEN 1 THEN t2.JAN
WHEN 2 THEN t2.FEB
WHEN 3 THEN t2.MAR
-- .....
WHEN 10 THEN t2.OCT
WHEN 11 THEN t2.NOV
WHEN 12 THEN t2.DEC
END
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.CurrencyCode = t2.CurrencyCode
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.