CASE 表达式 ent 为每个帐户返回 ELSE 值,即使条件为 True
CASE expression ent returning ELSE value for every account even when condition is True
即使 CASE 表达式逻辑为 True,下面的查询 returns 每个帐户都有两行,同时带有 'Active' 和 'Inactive' 标签。
例如。
如果一个帐户的 'revenue' 大于 0 并且 fiscal_qtr = '2022-Q1' 而不是仅返回 'Active' 它还会 returns 具有值 [=29= 的行].
SELECT
geo,
account,
subsegment,
forecast_group,
CASE
WHEN revenue > 0 and fiscal_qtr = '2022-Q1' then 'Active'
ELSE 'Inactive'
END AS "Active Acct"
FROM rev_planning.ace_global
WHERE
(fiscal_year_num between 2018 and 2021 or fiscal_qtr = '2022-Q1')
and revenue > 0
GROUP BY geo, account, subsegment, forecast_group
我试过将 case 表达式放在括号中,但它仍然 returns 两行用于所有应该返回的帐户 'Active'
(WHEN revenue > 0 and fiscal_qtr = '2022-Q1' then 'Active' ELSE 'Inactive'END) AS "Active Acct"
当前输出
geo
account
subsegment
forecast group
acitveacct
APAC
brothers neilsen
australia
dbiq
Active
APAC
brothers neilsen
australia
dbiq
Inactive
预期输出(因为根据案例表达式neilsen兄弟应该是“Active”
geo
account
subsegment
forecast group
acitveacct
APAC
brothers neilsen
australia
dbiq
Active
I have tried putting the case expression in parenthesis but it still
returns two rows for all of the accounts that should just be returning
'Active'
如果 SELECT returns 行持续多年和多个季度,这就是应该发生的情况。由于 CASE,当前年份和季度 (2022-Q1) 将被视为 'Active',而往年将被视为 'Inactive'.
听起来您只想 return 最近的一年。尝试使用 ROW_NUMBER() 对每个帐户的财政季度进行排序和排名。然后使用 where RowNum = 1
抓取最近的一个。下面的查询按 geo, account, subsegment
对结果进行分区,但您可以根据需要进行调整:
WITH cte AS (
SELECT *
, ROW_NUMBER() OVER(
PARTITION BY geo, account, subsegment
ORDER BY fiscal_year_num DESC, fiscal_qtr DESC
) AS RowNum
FROM ace_global
WHERE revenue > 0
OR ( fiscal_year_num BETWEEN 2018 AND 2021
OR fiscal_qtr = '2022-Q1'
)
)
SELECT geo
, account
, subsegment
, forecast_group
, CASE WHEN fiscal_qtr = '2022-Q1' THEN 'Active' ELSE 'Inactive' END AS "Active Acct"
FROM cte
WHERE RowNum = 1
示例数据
geo | account | subsegment | forecast_group | revenue | fiscal_year_num | fiscal_qtr
:-- | :---------- | :--------- | :------------- | ------: | --------------: | :---------
ABC | abc company | australia | dbiq | 50000 | 2018 | 2018-Q1
ABC | abc company | australia | dbiq | 1000000 | 2022 | 2022-Q1
EFG | efg company | australia | dbiq | 75000 | 2020 | 2020-Q2
HIJ | hij company | australia | dbiq | 787000 | 2021 | 2021-Q3
HIJ | hij company | australia | dbiq | 2000000 | 2022 | 2022-Q1
结果:
geo | account | subsegment | forecast_group | Active Acct
:-- | :---------- | :--------- | :------------- | :----------
ABC | abc company | australia | dbiq | Active
EFG | efg company | australia | dbiq | Inactive
HIJ | hij company | australia | dbiq | Active
db<>fiddle here
即使 CASE 表达式逻辑为 True,下面的查询 returns 每个帐户都有两行,同时带有 'Active' 和 'Inactive' 标签。
例如。 如果一个帐户的 'revenue' 大于 0 并且 fiscal_qtr = '2022-Q1' 而不是仅返回 'Active' 它还会 returns 具有值 [=29= 的行].
SELECT
geo,
account,
subsegment,
forecast_group,
CASE
WHEN revenue > 0 and fiscal_qtr = '2022-Q1' then 'Active'
ELSE 'Inactive'
END AS "Active Acct"
FROM rev_planning.ace_global
WHERE
(fiscal_year_num between 2018 and 2021 or fiscal_qtr = '2022-Q1')
and revenue > 0
GROUP BY geo, account, subsegment, forecast_group
我试过将 case 表达式放在括号中,但它仍然 returns 两行用于所有应该返回的帐户 'Active'
(WHEN revenue > 0 and fiscal_qtr = '2022-Q1' then 'Active' ELSE 'Inactive'END) AS "Active Acct"
当前输出
geo | account | subsegment | forecast group | acitveacct |
---|---|---|---|---|
APAC | brothers neilsen | australia | dbiq | Active |
APAC | brothers neilsen | australia | dbiq | Inactive |
预期输出(因为根据案例表达式neilsen兄弟应该是“Active”
geo | account | subsegment | forecast group | acitveacct |
---|---|---|---|---|
APAC | brothers neilsen | australia | dbiq | Active |
I have tried putting the case expression in parenthesis but it still returns two rows for all of the accounts that should just be returning 'Active'
如果 SELECT returns 行持续多年和多个季度,这就是应该发生的情况。由于 CASE,当前年份和季度 (2022-Q1) 将被视为 'Active',而往年将被视为 'Inactive'.
听起来您只想 return 最近的一年。尝试使用 ROW_NUMBER() 对每个帐户的财政季度进行排序和排名。然后使用 where RowNum = 1
抓取最近的一个。下面的查询按 geo, account, subsegment
对结果进行分区,但您可以根据需要进行调整:
WITH cte AS (
SELECT *
, ROW_NUMBER() OVER(
PARTITION BY geo, account, subsegment
ORDER BY fiscal_year_num DESC, fiscal_qtr DESC
) AS RowNum
FROM ace_global
WHERE revenue > 0
OR ( fiscal_year_num BETWEEN 2018 AND 2021
OR fiscal_qtr = '2022-Q1'
)
)
SELECT geo
, account
, subsegment
, forecast_group
, CASE WHEN fiscal_qtr = '2022-Q1' THEN 'Active' ELSE 'Inactive' END AS "Active Acct"
FROM cte
WHERE RowNum = 1
示例数据
geo | account | subsegment | forecast_group | revenue | fiscal_year_num | fiscal_qtr :-- | :---------- | :--------- | :------------- | ------: | --------------: | :--------- ABC | abc company | australia | dbiq | 50000 | 2018 | 2018-Q1 ABC | abc company | australia | dbiq | 1000000 | 2022 | 2022-Q1 EFG | efg company | australia | dbiq | 75000 | 2020 | 2020-Q2 HIJ | hij company | australia | dbiq | 787000 | 2021 | 2021-Q3 HIJ | hij company | australia | dbiq | 2000000 | 2022 | 2022-Q1
结果:
geo | account | subsegment | forecast_group | Active Acct :-- | :---------- | :--------- | :------------- | :---------- ABC | abc company | australia | dbiq | Active EFG | efg company | australia | dbiq | Inactive HIJ | hij company | australia | dbiq | Active
db<>fiddle here