0

我有一个问题,我正在使用的一张表的设置方式让我很难实现我需要的东西。

index_name  und_mnem   weight     percent_wt  undId   altId     type       
'.SXXP'      'AAL.L'  0.051341648  0.015464   18742  '08238P911 'CUS'
'.SXXP'      'AAL.L'  0.051341648  0.015464   18742  '32508392' 'ISIN'

我需要将这两行合并为一行,其中包含标题 CUS 和 ISIN 以及相应列中的正确 altId。

理想情况下,数据看起来像这样

index_name  und_mnem   weight     percent_wt  undId     Cus        ISIN      
'.SXXP'      'AAL.L'  0.051341648  0.015464   18742  '08238P911 '32508392'

这是我的查询

SELECT 
    il.index_name, il.und_mnem, il.weight, il.percent_wt, il.index_shares, 
    il.undId, aa.altId, aa.type 
FROM 
    indexlist il 
join 
    altIdentifier aa on aa.id = il.undId 
where 
    il.index_name = ".SXXP" and type in ("ISIN","CUS")

我正在使用 Sybase

任何帮助将不胜感激!

谢谢,

4

2 回答 2

0

您应该能够使用带有CASE表达式的聚合函数来获得结果:

SELECT  il.index_name, 
  il.und_mnem, 
  il.weight, 
  il.percent_wt, 
  il.index_shares, 
  il.undId, 
  max(case when aa.type = 'CUS' then aa.altId end) Cus,
  max(case when aa.type = 'ISIN' then aa.altId end) ISUN
FROM indexlist il 
join altIdentifier aa 
  on aa.id = il.undId 
where il.index_name = ".SXXP" 
  and type in ("ISIN","CUS")
group by il.index_name, il.und_mnem, il.weight, il.percent_wt, 
  il.index_shares, il.undId
于 2013-05-17T12:34:09.160 回答
0
select il.index_name, il.und_mnem, il.weight, il.percent_wt, il.index_shares, 
        il.undId, cusTbl.cus,isinTbl.isin
from indexlist il join
(select il.undId,aa.altId as cus 
from indexlist il 
    join 
        altIdentifier aa on aa.id = il.undId 
    where 
        il.index_name = ".SXXP" and type="CUS") as cusTbl on il.undId=cusTbl.undId
join
(select il.undId,aa.altId as isin 
from indexlist il 
    join 
        altIdentifier aa on aa.id = il.undId 
    where 
        il.index_name = ".SXXP" and type="ISIN") as isinTbl on il.undId=isinTbl.undId
于 2013-05-17T12:16:23.877 回答