简体   繁体   English

Postgres SQL 在 BigQuery 中聚合查询?

[英]Postgres SQL aggregates query in BigQuery?

For Below Postgres SQL query, I do use PIVOT in BigQuery, beside PIVOT, any other method for such query in BigQuery?对于以下 Postgres SQL 查询,我在 BigQuery 中使用 PIVOT,除了 PIVOT,BigQuery 中此类查询还有其他方法吗?

-- Postgres SQL --
    SELECT 
       Apple, 
       Orange,
       Lemon,
       CASE WHEN Apple >= 50 THEN 1 ELSE 0 END AS Apple50
       CASE WHEN Orange >= 50 THEN 1 ELSE 0 END AS Orange50
       CASE WHEN Lemon >= 50 THEN 1 ELSE 0 END AS Lemon50
    FROM (
       SELECT td.timestamp,
          COALESCE(MAX(td.value) FILTER (WHERE attribute_id = 16), 0) as Apple, 
          COALESCE(MAX(td.value) FILTER (WHERE attribute_id = 17), 0) as Orange, 
          COALESCE(MAX(td.value) FILTER (WHERE attribute_id = 18), 0) as Lemon
       FROM TableData td
       WHERE td.attribute_id IN (16, 17, 18) 
       GROUP BY td.timestamp
       ORDER BY timestamp;
    ) AS td2

-- My attempt BigQuery Query -- 
SELECT
    value_16 as Apple,
    value_17 as Orange,
    value_18 as Lemon,
    CASE WHEN value_16 >= 50 THEN 1 ELSE 0 END as Apple50
    CASE WHEN value_17 >= 50 THEN 1 ELSE 0 END as Orange50
    CASE WHEN value_18 >= 50 THEN 1 ELSE 0 END AS Lemon50
FROM (
        SELECT * FROM(
            SELECT 
                timestamp,
                attribute_id,
                value
            FROM `PROJECT_ID.DB_NAME.FRUITS` as td
            WHERE td.attribute_id IN (16,17,18)
        )PIVOT
        (
            MAX(value) as value
            FOR attribute_id IN (16,17,18)
        )
)as td2

Below is the sample relation of the table.下面是表格的示例关系。

-- TableData --
attribute_id  | value     | timestamp  |
--------------+-----------+------------+
17            | 100       | 1618822794 |
17            | 100       | 1618822861 |
16            | 50        | 1618822794 |
16            | 50        | 1618822861 |

-- TableAttribute --
id            | name     |
--------------+----------+
16            | Apple    |
17            | Orange   |
18            | Lemon    |

-- Expected Result --
timestamp     | Apple   | Orange | Lemon | Apple50 | Orange50 | Lemon50 |
--------------+---------+--------+-------+---------+----------+---------+
1618822794    | 50      | 100    | 0     | 1       | 1        | 0
1618822861    | 50      | 100    | 0     | 1       | 1        | 0

Pivot is likely the best way to achieve what you're wanting. Pivot 可能是实现您想要的目标的最佳方式。 Consider the following approach though as it might be simpler to manage:请考虑以下方法,因为它可能更易于管理:

with aggregate_data as (
    select td.timestamp
        , ta.name
        , td.value as value 
    from TableData td
    full outer join TableAttribute ta
    on td.attribute_id = ta.id
)

select timestamp
    , value_Apple as Apple
    , value_Orange as Orange
    , value_Lemon as Lemon
    , _50_Apple as Apple50
    , _50_Orange as Orange50
    , _50_Lemon as Lemon50
from aggregate_data
pivot(max(value) value, max(case when value >=50 then 1 else 0 end) _50 for name in ('Apple', 'Orange', 'Lemon'))
where timestamp is not null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM