假设我有以下示例输入:
WITH Ratings AS (
(SELECT 'A' name, 2 score) UNION ALL
(SELECT 'B' name, 0 score) UNION ALL
(SELECT 'C' name, 5 score) UNION ALL
(SELECT 'D' name, 1 score))
Where score
是 0 到 5 之间的数字。
如何生成显示名称和相应星星数量的报告?
我们可以使用两个 Unicode 字符将星级评级构建为字符串:
★ - Unicode code point 9733
☆ - Unicode code point 9734
我们可以用CODE_POINTS_TO_STRING https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#code_points_to_string构建恒星的功能,以及REPEAT https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#repeat产生正确数量的星星的函数
将样本输入的解决方案组合在一起将是:
WITH Ratings AS (
(SELECT 'A' name, 2 score) UNION ALL
(SELECT 'B' name, 0 score) UNION ALL
(SELECT 'C' name, 5 score) UNION ALL
(SELECT 'D' name, 1 score))
SELECT
name,
CONCAT(
REPEAT(CODE_POINTS_TO_STRING([9733]), score),
REPEAT(CODE_POINTS_TO_STRING([9734]), 5-score)) score
FROM Ratings
它将产生以下结果:
name score
A ★★☆☆☆
B ☆☆☆☆☆
C ★★★★★
D ★☆☆☆☆
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)