即使条件为true,案例表达式将为每个帐户返回其他帐户的其他值
即使 CASE 表达式逻辑为 True,以下查询也会为每个具有“活动”和“非活动”标签的帐户返回两行。
前任。 如果账户的“收入”大于 0,并且财政季度=“2022-Q1”,它不仅返回“活动”,还会返回值为“非活动”的行。
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 表达式放在括号中,但它仍然为所有应该返回“活动”的帐户返回两行
(WHEN revenue > 0 and fiscal_qtr = '2022-Q1' then 'Active' ELSE 'Inactive'END) AS "Active Acct"
当前输出
geo | 帐户 | 子分段 | 预测组 | acitveacct |
---|---|---|---|---|
APAC | brothers neilsen | australia | dbiq | Active |
APAC | brothers neilsen | australia | dbiq | Inactive |
Expected Output (因为兄弟尼尔森应该根据案例表达式
地理 | 帐户子 | 细分 | 预测组 | acitveacct |
---|---|---|---|---|
APAC | 兄弟尼尔森 | 为“活跃”澳大利亚 | dbiq | 活跃 |
The below query returns two rows for every account with both the 'Active' and 'Inactive' labels even when the CASE expression logic is True.
ex.
If an account has 'revenue' greater than 0 and the fiscal_qtr = '2022-Q1' instead of returning just 'Active' it also returns a row with the value 'Inactive'.
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
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'
(WHEN revenue > 0 and fiscal_qtr = '2022-Q1' then 'Active' ELSE 'Inactive'END) AS "Active Acct"
Current Output
geo | account | subsegment | forecast group | acitveacct |
---|---|---|---|---|
APAC | brothers neilsen | australia | dbiq | Active |
APAC | brothers neilsen | australia | dbiq | Inactive |
Expected Output (because brothers neilsen should be "Active" based on the case expression
geo | account | subsegment | forecast group | acitveacct |
---|---|---|---|---|
APAC | brothers neilsen | australia | dbiq | Active |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是如果选择将行返回多年和季度,那应该发生。由于当年和季度(2022-Q1),将被视为'Active',并且以前的几年将是'Intactive'。
听起来您只想返回最近一年。尝试使用 row_number()帐户。然后使用
其中Rownum = 1
抓住最新的。下面的查询通过geo,帐户,子段
结果,但是您可以根据需要进行调整:示例数据
结果:
db<>>
That's what should happen if the SELECT returns rows for multiple years and quarters. Due to the CASE the current year and quarter (2022-Q1) would be considered 'Active' and prior years would be 'Inactive'.
It sounds like you only want to return the most recent year. Try using ROW_NUMBER() to sort and rank the fiscal quarters for each account. Then use
where RowNum = 1
to grab the most recent one. The query below partitions results bygeo, account, subsegment
, but you can adjust it as needed:Sample Data
Results:
db<>fiddle here