即使条件为true,案例表达式将为每个帐户返回其他帐户的其他值

发布于 2025-01-17 22:58:03 字数 1301 浏览 0 评论 0原文

即使 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
APACbrothers neilsenaustraliadbiqActive
APACbrothers neilsenaustraliadbiqInactive

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

geoaccountsubsegmentforecast groupacitveacct
APACbrothers neilsenaustraliadbiqActive
APACbrothers neilsenaustraliadbiqInactive

Expected Output (because brothers neilsen should be "Active" based on the case expression

geoaccountsubsegmentforecast groupacitveacct
APACbrothers neilsenaustraliadbiqActive

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2025-01-24 22:58:03

我尝试将案例表达式放在括号中,但仍然
返回所有应该返回的所有帐户的两行
'Active'

这就是如果选择将行返回多年和季度,那应该发生。由于当年和季度(2022-Q1),将被视为'Active',并且以前的几年将是'Intactive'

听起来您只想返回最近一年。尝试使用 row_number()帐户。然后使用其中Rownum = 1抓住最新的。下面的查询通过geo,帐户,子段结果,但是您可以根据需要进行调整:

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 |帐户|子段| forecast_group |收入| fiscal_year_num | fiscal_qtr
: -  | :----------- | :---------- | :--------------- | ------:| -----------------:| :--------------------
ABC | ABC公司|澳大利亚| dbiq | 50000 | 2018 | 2018-Q1   
ABC | ABC公司|澳大利亚| dbiq | 1000000 | 2022 | 2022-Q1   
EFG | EFG公司|澳大利亚| dbiq | 75000 | 2020 | 2020-Q2   
hij | HIJ Company |澳大利亚| dbiq | 787000 | 2021 | 2021-Q3   
hij | HIJ Company |澳大利亚| dbiq | 2000000 | 2022 | 2022-Q1   

结果:

 geo |帐户|子段| forecast_group |主动acct
: -  | :----------- | :---------- | :--------------- | :------------
ABC | ABC公司|澳大利亚| dbiq |积极的     
EFG | EFG公司|澳大利亚| dbiq |不活动   
hij | HIJ Company |澳大利亚| dbiq |积极的     

db<>>

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'

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 by geo, account, subsegment, but you can adjust it as needed:

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

Sample Data

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   

Results:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文