如何在ClickHouse中将具有相同列值的mysql行分组为一行?

时间:2020-06-17 14:06:23

标签: sql clickhouse

我在ClickHouse中使用此查询:

SELECT DISTINCT
  iap.EventId,
  iap.Id as Title,
  ole.RewardId,
  ole.RewardGain,
  if((ole.RewardType = 'Currency' and ole.RewardId = 'Cash'), ole.RewardGain, null) as Soft
FROM OpenLootboxEvent ole 
ANY LEFT OUTER JOIN InAppPurchaseEvent iap
ON ole.EventSourceId = iap.EventId
WHERE iap.Id in ('superHotDeal');

此查询返回结果:

EventId  Title         RewardId  Gain  
---------------------------------------------------------------------
111      superHotDeal  m3        5    
111      superHotDeal  m14       13         
111      superHotDeal  m20       25         
111      superHotDeal  Cash      282            
111      superHotDeal  Talent    null   
111      superHotDeal  Hard      null  

每个“ EventId”和“ Title”具有6个含义,其中“ RewardId”的值可以从“ m1”到“ m26”,并且始终可以具有“现金”,“人才”,“硬”值。

如何在同一行中检索具有相同“ EventId”和“ Title”的所有行?

赞:

EventId  Title         man1  man1Cards   man2  man2Cards   man3  man3Cards  Cash  Talent  Hard                     
----------------------------------------------------------------------------------------------
111      superHotDeal  m3  5             m14   13          m20   25         282   null    null

1 个答案:

答案 0 :(得分:0)

select EventId, Title, groupArray(tuple(RewardId, RewardGain))
from (
SELECT DISTINCT
  iap.EventId EventId,
  iap.Id as Title,
  ole.RewardId RewardId,
  ole.RewardGain RewardGain,
  if((ole.RewardType = 'Currency' and ole.RewardId = 'Cash'), ole.RewardGain, null) as Soft
FROM OpenLootboxEvent ole 
ANY LEFT OUTER JOIN InAppPurchaseEvent iap
ON ole.EventSourceId = iap.EventId
WHERE iap.Id in ('superHotDeal'))
group by EventId, Title

Pivot In clickhouse

相关问题