我有一个表
POTENTIAL_MASTER
,其中存储了每个 PRODUCT
的潜力。
我在
PRODUCT
栏中有一个产品列表,如下所示:
ENGLISH, TAMIL, HINDI, URDU, TELUGU
泰米尔语和泰卢固语的潜力始终相同。如果潜力是泰米尔语,它也适用于泰卢固语。
现在,当我计算所有产品的潜力总和时,我应该只得到泰米尔语和泰卢固语的一个值。例如:泰米尔语和泰卢固语的潜力为 20。查询应仅考虑一个 20 值。
有人能指导我该怎么做吗?
我尝试了不同的联合声明,但潜力被利用了两次。
我想要只考虑泰米尔语或泰卢固语一个值的潜力总和
您似乎想将泰米尔语和泰卢固语视为可互换的。
因此,我们可以创建它们的 CTE,替换为单个值并分配相同的
potential
值 20。
WITH
--create a table of POTENTIAL_MASTER since it hasn't been supplied
POTENTIAL_MASTER AS (
SELECT "ENGLISH" AS product
UNION ALL
SELECT "TAMIL"
UNION ALL
SELECT "HINDI"
UNION ALL
SELECT "URDU"
UNION ALL
SELECT "TELUGU"
),
--treat TAMIL and TELUGU as the same product
--assign all products a potential of 20
POTENTIAL_MASTER_CLEAN AS (
SELECT DISTINCT
#product AS product_orig,
CASE
WHEN product in ("TAMIL","TELUGU") THEN "TAMIL/TELUGU"
ELSE product
END AS product_new,
CAST(20 AS INT64) AS potential -- all products have potential of 20
FROM POTENTIAL_MASTER
)
SELECT *
FROM POTENTIAL_MASTER_CLEAN