【问题标题】:join with 2 different sizes arrays加入 2 个不同大小的数组
【发布时间】:2021-08-12 00:13:20
【问题描述】:

如何修改查询以从 ClickHouse 表中提取 2 个不同大小的数组中的数据? 目前我有以下正常工作的查询:

   SELECT
   concat(toString(ApplicationID),': ',dictGetString('Application','Name',CAST(ApplicationID as 
   UInt64))) as site,
   concat(toString(SpotID),': ',dictGetString('Spot','Name',CAST(SpotID as UInt64))) as `Spot`,
   dictGetString('Domain', 'Value', DomainMap.ID) AS sourceDomain,
   toFloat64(sum(DomainMap.Impressions)) as cnt,
   SUM(DomainMap.Price/1000000000) as p,
   SUM(DomainMap.Revenue/1000000000) as r,
   p - r as res
   FROM distributed.publishers_map array Join DomainMap
   WHERE ActionDate = '2021-05-16' and ApplicationID in (4749)
   GROUP BY
   DomainMap.ID,
   ApplicationID,SpotID
   ORDER BY cnt DESC

然后我需要从名为 ExternalSpotMap 的类似数组中添加数据。从这个数组中我也需要得到

   SUM(ExternalSpotMap.Price/1000000000) as p,
   SUM(ExternalSpotMap.Revenue/1000000000) as r,
   p - r as res

并从上述现有查询中添加数据。 问题是这 2 个数组 - DomainMap 和 ExternalSpotMap 的大小不同,我不能只进行以下操作:

FROM distributed.publishers_map array Join DomainMap, ExternalSpotMap

选项2。如果我做以下:

SELECT *
   from
   (
   Select *
   from
   (
   SELECT
   concat(toString(ApplicationID),': ',dictGetString('Application','Name',CAST(ApplicationID as 
   UInt64))) as site,
   concat(toString(SpotID),': ',dictGetString('Spot','Name',CAST(SpotID as UInt64))) as `Spot`,
   dictGetString('Domain', 'Value', DomainMap.ID) AS sourceDomain,
   toFloat64(sum(DomainMap.Impressions)) as cnt,
   SUM(DomainMap.Price/1000000000) as p,
   SUM(DomainMap.Revenue/1000000000) as r,
   p - r as res,
   SUM(ExternalSpotMap.Price/1000000000) as esmp,
   SUM(ExternalSpotMap.Revenue/1000000000) as esmr,
   esmp - esmr as res2
   FROM distributed.publishers_map
   WHERE ActionDate = '2021-05-16' and ApplicationID in (4749)
   GROUP BY DomainMap.ID, ApplicationID,SpotID
   ORDER BY cnt DESC
   )
   array join DomainMap
   )
   ARRAY JOIN ExternalSpotMap

然后我得到一个错误:

SQL Error [43]: ClickHouse exception,  Code: 43, e.displayText() = 
DB::Exception: Illegal type Array(Int64) of argument for aggregate function 
sum 

问题的原因可能是什么?

【问题讨论】:

    标签: sql arrays join clickhouse


    【解决方案1】:
    SELECT *
    FROM
    (
        SELECT *
        FROM
        (
            SELECT
                1,
                [1, 2, 3] AS a,
                ['a', 'b'] AS b
        )
        ARRAY JOIN a
    )
    ARRAY JOIN b
    
    
    ┌─1─┬─a─┬─b─┐
    │ 1 │ 1 │ a │
    │ 1 │ 1 │ b │
    │ 1 │ 2 │ a │
    │ 1 │ 2 │ b │
    │ 1 │ 3 │ a │
    │ 1 │ 3 │ b │
    └───┴───┴───┘
    

    【讨论】:

    • 你好@Denny Crane。感谢您的关注。我在上面的请求中在“选项 2”主题下添加了结果。你能看看那里吗?我是否按照您的计划重新创建查询?
    猜你喜欢
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2012-07-28
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多