1

我想将在不同行上输出的一对多关系合并到一行。

(select rate_value1 
      FROM xgenca_enquiry_event 
       INNER JOIN  xgenca_enquiry_iso_code_translation 
             ON 
       xgenca_enquiry_event_rate.rate_code_id 
           = xgenca_enquiry_iso_code_translation.id  
       where xgenca_enquiry_event_rate.event_id = xgenca_enquiry_event.id 
              and ISO_code = 'PDIV') as PDIVrate, 
(select rate_value1 
       FROM xgenca_enquiry_event 
        INNER JOIN xgenca_enquiry_iso_code_translation 
              ON 
         xgenca_enquiry_event_rate.rate_code_id 
           = xgenca_enquiry_iso_code_translation.id  
        where xgenca_enquiry_event_rate.event_id = xgenca_enquiry_event.id 
              and ISO_code = 'TAXR') as TAXrate

PDIVrate    TAXrate
NULL        10.0000000
0.0059120   NULL

我想要一行的结果。任何帮助将不胜感激。谢谢。

4

2 回答 2

2

您可以使用聚合函数来执行此操作:

select 
  max(case when ISO_code = 'PDIV' then rate_value1 end) PDIVRate,
  max(case when ISO_code = 'TAXR' then rate_value1 end) TAXRate
FROM xgenca_enquiry_event_rate r 
INNER JOIN  xgenca_enquiry_iso_code_translation t
  ON r.rate_code_id = t.id  
INNER JOIN xgenca_enquiry_event e
  ON r.event_id = e.id 

看起来您要加入的三个表在查询中是相同的。这使用连接将其合并为单个查询。

于 2013-01-18T10:20:56.333 回答
0

看这里: 我可以用逗号将多行分隔成一列吗?

使用 STUFF 在 SQL Server 中模拟 Oracle 的 LISTAGG():

SELECT Column1,
  stuff((
   SELECT ', ' + Column2
     FROM tableName as t1
      where t1.Column1 = t2.Column1
       FOR XML PATH('')
     ), 1, 2, '')
 FROM tableName as t2
GROUP BY Column1
/

从这里复制:https ://github.com/jOOQ/jOOQ/issues/1277

于 2013-01-18T15:07:35.010 回答