如何在标准SQL大查询中将一列拆分为多列

时间:2018-08-28 08:05:47

标签: sql google-bigquery

使用Google BigQuery

if(bShouldElementShow){
 jQueryitem.fadeIn();
 jQuery('.show').clone().contents().appendTo('.results').wrap( "<div class='new'></div>" );
}
else {
 jQueryitem.hide();
}

这是表格的外观:

This is how the table looks

预期输出:

Expected Output

  • “日期”列必须分为多个列,每个列代表一个月中的一天,并且应包含不同“游戏”和“地理位置”的收入总和。

  • 此外,必须创建一个称为“总计”的列,该列提供当月游戏广告地理区域的总收入。请提出一种实现方法

1 个答案:

答案 0 :(得分:1)

要使用BigQuery进行数据透视,可以使用...

SELECT
  Game,
  Geo,
  Category,
  SUM(CASE WHEN EXTRACT(DAY FROM Date) = 01 THEN Revenue ELSE 0 END)   AS DAY_OF_MONTH_01,
  SUM(CASE WHEN EXTRACT(DAY FROM Date) = 02 THEN Revenue ELSE 0 END)   AS DAY_OF_MONTH_02,
  SUM(CASE WHEN EXTRACT(DAY FROM Date) = 03 THEN Revenue ELSE 0 END)   AS DAY_OF_MONTH_03,
  ...
  SUM(CASE WHEN EXTRACT(DAY FROM Date) = 30 THEN Revenue ELSE 0 END)   AS DAY_OF_MONTH_30,
  SUM(CASE WHEN EXTRACT(DAY FROM Date) = 31 THEN Revenue ELSE 0 END)   AS DAY_OF_MONTH_31,
  SUM(Revenue)                                                         AS GRAND_TOTAL
FROM
  yourSource
WHERE
      Date >= DATE '2018-08-01'
  AND Date <  DATE '2018-09-01'
GROUP BY
  Game,
  Geo,
  Category

然后,列标题不包含月份,但这是因为它们不能从数据中得出。我仍然认为您应该使用此查询作为源来使用电子表格...

SELECT
  Game,
  Geo,
  Category,
  Date,
  SUM(Revenue)   AS Revenue
FROM
  yourSource
WHERE
      Date >= DATE '2018-08-01'
  AND Date <  DATE '2018-09-01'
GROUP BY
  Game,
  Geo,
  Category,
  Date

然后您就无法拥有“巨大”数据,只有您需要的数据,然后让工作表将其旋转...