我正在尝试将Table1连接到Table2,如下所示:
表1:
| RecCurr | PayCurr | MTMCurr | TradeID |
|---------|---------|---------|---------|
| USD | CAD | JPY | 1234 |
表2:
+------+-------+
| Curr | Value |
+------+-------+
| USD | 10 |
| CAD | 11 |
| JPY | 12 |
+------+-------+
加入表:
+---------+---------+---------+---------+----------+----------+----------+
| RecCurr | PayCurr | MTMCurr | TradeID | RecValue | PayValue | MTMValue |
+---------+---------+---------+---------+----------+----------+----------+
| USD | CAD | JPY | 1234 | 10 | 11 | 12 |
+---------+---------+---------+---------+----------+----------+----------+
到目前为止,我唯一的解决方案如下:
SELECT T1.RecCurr, T1.PayCurr, T1.MTMCurr, T2.RecValue, T3.PayValue, T4.MTMValue
FROM
(SELECT RecCurr, PayCurr, MTMCurr FROM Table1) T1,
(SELECT RecCurr, RecValue FROM Table2) T2,
(SELECT PayCurr, PayValue FROM Table2) T3,
(SELECT MTMCurr, MTMValue FROM Table2) T4
where T1.RecCurr = T2.RecCurr
and T1.PayCurr = T3.PayCurr
and T4.MTMCurr = T4.MTMCurr
是否有一个更清洁的解决方案,不需要我使用Table1连接Table2 3次?
答案 0 :(得分:0)
当FROM
子句中有逗号时,总是是编写查询的更好方法。此外,子查询是不必要的:
SELECT T1.RecCurr, T1.PayCurr, T1.MTMCurr,
T2r.RecValue, T2p.PayValue, T2m.MTMValue
FROM T1 JOIN
Table2 t2r
ON T2r.RecCurr = T1.RecCurr JOIN
Table2 t2p
ON t2p.PayCurr = t1.PayCurr
Table2 t2m
ON t2m.MTMCurr = t1.MTMCurr;
答案 1 :(得分:0)
注意:以下适用于ASE;我认为它应该适用于SQLAnywhere(和IQ),但我现在还没有运行SQLAnywhere数据库......
如果您只想对Table2进行一次连接,那么您将需要一个执行表轴的查询。
首先是设置:
create table Table1
(RecCurr char(3)
,PayCurr char(3)
,MTMCurr char(3)
,TradeID int)
go
insert Table1 values ('USD','CAD','JPY',1234)
go
create table Table2
(Curr char(3)
,Value int)
go
insert Table2 values ('USD',10)
insert Table2 values ('CAD',11)
insert Table2 values ('JPY',12)
go
select * from Table1
go
RecCurr PayCurr MTMCurr TradeID
------- ------- ------- -----------
USD CAD JPY 1234
select * from Table2
go
Curr Value
---- -----------
USD 10
CAD 11
JPY 12
使用表2的单个连接的示例查询:
select T1.RecCurr,
T1.PayCurr,
T1.MTMCurr,
T1.TradeID,
sum(case when T2.Curr = T1.RecCurr then T2.Value else 0 end) as RecValue,
sum(case when T2.Curr = T1.PayCurr then T2.Value else 0 end) as PayValue,
sum(case when T2.Curr = T1.MTMCurr then T2.Value else 0 end) as MTMValue
from Table1 T1,
Table2 T2
where T2.Curr in (T1.RecCurr, T1.PayCurr, T1.MTMCurr)
group by T1.RecCurr, T1.PayCurr, T1.MTMCurr, T1.TradeID
go
RecCurr PayCurr MTMCurr TradeID RecValue PayValue MTMValue
------- ------- ------- ----------- ----------- ----------- -----------
USD CAD JPY 1234 10 11 12
对于那些对逗号过敏的人(在FROM
条款中):
select T1.RecCurr,
T1.PayCurr,
T1.MTMCurr,
T1.TradeID,
sum(case when T2.Curr = T1.RecCurr then T2.Value else 0 end) as RecValue,
sum(case when T2.Curr = T1.PayCurr then T2.Value else 0 end) as PayValue,
sum(case when T2.Curr = T1.MTMCurr then T2.Value else 0 end) as MTMValue
from Table1 T1
join Table2 T2
on T2.Curr in (T1.RecCurr, T1.PayCurr, T1.MTMCurr)
group by T1.RecCurr, T1.PayCurr, T1.MTMCurr, T1.TradeID
go
RecCurr PayCurr MTMCurr TradeID RecValue PayValue MTMValue
------- ------- ------- ----------- ----------- ----------- -----------
USD CAD JPY 1234 10 11 12