SQL: 如何旋转列中的维度?

SQL: how to pivot dimensions in a column?

我想做以下事情:

原始table:这只有两篇文章,它们的流量渠道和唯一浏览量

SELECT * FROM website_content_table

Returns:

我想要 return 一个 table 看起来像这样的:

这是我目前的解决方案——我认为它既不优雅又缓慢。在 BigQuery SQL 中肯定有更简单的方法来执行此操作(注意 - 我没有可以使用的数据透视函数)

SELECT pageTitle, 
      sum(unique_pageviews) AS unique_pageviews, 
      sum(organic_unique_pageviews) AS organic_unique_pageviews,
      sum(email_unique_pageviews) AS email_unique_pageviews,
      sum(paid_unique_pageviews) AS paid_unique_pageviews     
FROM (
             -- table for total unique_pageviews
            (SELECT pageTitle, 
                   sum(unique_pageviews) unique_pageviews
            FROM website_content_table 
            GROUP BY pageTitle) AS a

            -- table for organic  unique_pageviews
            LEFT JOIN 
                    (SELECT pageTitle,
                            sum(unique_pageviews) organic_unique_pageviews
                     FROM website_content_table 
                     WHERE traffic_channel = 'Organic'
                     GROUP BY pageTitle) AS organic
            ON a.pageTitle = organic.pageTitle  

            -- table for email  unique_pageviews
            LEFT JOIN 
                    (SELECT pageTitle,
                            sum(unique_pageviews) email_unique_pageviews
                     FROM website_content_table 
                     WHERE traffic_channel = 'Email'
                     GROUP BY pageTitle) AS email
            ON a.pageTitle = email.pageTitle  

            -- table for paid  unique_pageviews
            LEFT JOIN 
                    (SELECT pageTitle,
                            sum(unique_pageviews) paid_unique_pageviews
                     FROM website_content_table 
                     WHERE traffic_channel = 'Paid'
                     GROUP BY pageTitle) AS paid
            ON a.pageTitle = paid.pageTitle  
)
GROUP BY pageTitle
ORDER BY unique_pageviews

使用带条件聚合的数据透视查询有一种更简单的方法:

SELECT
    pageTitle,
    SUM(unique_pageviews) AS unique_pageviews,
    SUM(CASE WHEN traffic_channel = 'Organic' THEN unique_pageviews ELSE 0 END) AS Organic,
    SUM(CASE WHEN traffic_channel = 'Email'   THEN unique_pageviews ELSE 0 END) AS Email,
    SUM(CASE WHEN traffic_channel = 'Paid'    THEN unique_pageviews ELSE 0 END) AS Paid
FROM yourTable
GROUP BY
    pageTitle;

如果列值是固定的,您可以使用条件聚合 -

SELECT pageTitle
      ,SUM(unique_pageviews) unique_pageviews
      ,MAX(CASE WHEN traffic_channel = 'Organic' THEN unique_pageviews END) AS Organic
      ,MAX(CASE WHEN traffic_channel = 'Email' THEN unique_pageviews END) AS Email
      ,MAX(CASE WHEN traffic_channel = 'Paid' THEN unique_pageviews END) AS Paid
FROM website_content_table
GROUP BY pageTitle;