简体   繁体   English

创建案例表达时出现的问题

[英]Issue while creating case expression

I am facing issues when I try to create a case expression with a group function condition along with normal column conditions. 当我尝试创建带有组函数条件以及常规列条件的case表达式时,我遇到了问题。

Table: STG 表:STG

+ -------+--------+------+----------+
| Ref_ID | Actual | Paid | Reason   |
+ -------+--------+------+----------+
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 0      | 480  | TRAINING |
| H1     | 0      | 0    |          |
| H1     | 360    | 0    |          |
| H1     | 360    | 0    |          |
+ -------+--------+------+----------+

Already the code is like below: 代码已经如下所示:

Explanation: If Actual hrs per day exceeds 480 then that needs to be taken as 480. ie. 说明:如果每天的实际小时数超过480,则需要将其视为480。 8hrs/day * 60 =480. 8小时/天* 60 = 480。 then the sum of actual is extended 1440 then that needs to be deducted from the sum of actual hrs. 然后将实际总和扩展1440,然后需要从实际总和中扣除。

In this same query, I have to sum the all actuals along with paid (paid hrs if reason is Training) 在同一查询中,我必须将所有实际值与已支付的总和(如果原因为培训,则为已支付的小时数)

 SELECT
    CASE WHEN (SUM(CASE WHEN (ACTUAL)>480 THEN 480 ELSE (ACTUAL) END))>1440 THEN
    ((SUM(CASE WHEN (ACTUAL)>480 THEN 480 ELSE (ACTUAL) END))-1440) ELSE 0
    END SINGLE_RATE
    FROM STG WHERE REF_ID='H1'
    GROUP BY REF_ID;

I tried to modify like below: 我试图像下面这样修改:

SELECT
    CASE WHEN (SUM(CASE WHEN (ACTUAL)>480 THEN 480 ELSE (ACTUAL) END)+(case when reason = 'training' then paid else 0 end ))>1440 THEN
    ((SUM(CASE WHEN (ACTUAL)>480 THEN 480 ELSE (ACTUAL) END)+(case when reason = 'training' then paid else 0 end ))-1440) ELSE 0
    END SINGLE_RATE
    FROM STG WHERE REF_ID='H1'
    GROUP BY REF_ID;

but getting below error: 但出现以下错误:

    ORA-00979: not a GROUP BY expression
    00979. 00000 -  "not a GROUP BY expression"
    *Cause:
    *Action:

Also tried like: 也尝试过:

SELECT
    (sum ( (CASE WHEN (ACTUAL)>480 THEN 480 ELSE (ACTUAL) END) + case when reason = 'training' then paid else 0 end )-1440) SINGLE_RATE
    FROM STG WHERE REF_ID='H1'
    GROUP BY REF_ID
    having sum ( (CASE WHEN (ACTUAL)>480 THEN 480 ELSE (ACTUAL) END) + case when reason = 'training' then paid else 0 end )>1440;

but the paid amount is not getting calculated. 但未计算出paid金额。 Only actual is added. actual添加。

Please give your suggestions. 请提出您的建议。

From your business rules, you always want to include actual in the sum() calculation but only include paid when the reason is 'training' . 根据您的业务规则,您始终希望在sum()计算中包括actual值,但是仅当原因为'training'时才包括paid That rule is quite easy to express in a case statement. 该规则很容易在case陈述中表达。 In addition you have a cap that actual cannot exceed 480. That rule can be satisfied with a least() function: 另外,您有一个actual不能超过480的上限。该规则可以用minimum least()函数满足:

select ref_id
      , sum ( least(actual, 480) + case when reason = 'training' then paid else 0 end ) as tot
from stg
group by ref_id
/

To be frank I am still not clear what you are trying to achieve with the 1440; 坦率地说,我仍然不清楚您要使用1440实现的目标。 life would be easier if you had posted some sample data which covered all the cases you wanted to handle and your desired output derived from that sample data. 如果您发布了一些示例数据,其中涵盖了您要处理的所有情况,并且您从该示例数据中获得了所需的输出,那么生活将会更加轻松。 But I am going to guess anyway: 但是我还是要猜测:

with cte as (
    select ref_id
          , sum ( least(actual, 480) + case when reason = 'training' then paid else 0 end ) as tot
    from stg
    group by ref_id
    )
select ref_id
       case 
          when tot <= 1440 then tot
          else tot - 1440
       end as adjusted_tot
from cte
/

As before, if this does not provide the answer you want please edit your question. 和以前一样,如果没有提供您想要的答案,请编辑您的问题。 The clearer your requirement the more likely you are to get a timely answer. 您的要求越明确,您越有可能及时得到答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM