使用SQL将指标分组到维度 - DataStudio

时间:2018-05-14 16:19:26

标签: sql google-data-studio

我正在尝试将指标分组为维度,如下所示:

+--------+-------+
|  Meal  | Score |
+--------+-------+
| Burger |     4 |
| Chips  |     4 |
| Beans  |     2 |
+--------+-------+

+-------+-------+
| Score | Count |
+-------+-------+
|     1 |     0 |
|     2 |     1 |
|     3 |     0 |
|     4 |     2 |
|     5 |     0 |
+-------+-------+

然后,我将能够在数据工作室中进一步对这些进行分组,如下所示:

Low Score (1-3)
High Score (4-5)

我在我的桌面上尝试了以下SQL查询,但它出现了错误:

SELECT COUNT(Meal), Score
FROM [table]
GROUP BY Score

2 个答案:

答案 0 :(得分:2)

你有没有尝试过:

SELECT Score, COUNT(Score) as Count
FROM table
GROUP BY Score
ORDER BY Score

答案 1 :(得分:0)

为将来提供参考,添加了一个Google Data Studio(也带有标签)建议,以使用原始数据集实现结果。

下面的CASE语句将Score在1到3之间分为Low Score,在4到5之间分为High Score,而ELSE值则分为Unclassified Score

CASE
  WHEN Score >= 1 AND Score <= 3 THEN "Low Score"
  WHEN Score >= 4 AND Score <= 5 THEN "High Score"
  ELSE "Unclassified Score"
END

Google Data Studio Report和要详细说明的GIF: