2017-08-02 177 views
0

我想加入到表1表2,如下图所示:加入3列从一个表到1列另一个表

表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 

是否有不需要我与表1表2加入3次清洁的解决方案?

+0

为什么表2中的行变成列? –

+0

哪个Sybase产品(ASE,SQLAnywhere,IQ,Advantage)? – markp

回答

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 
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; 
相关问题