满足条件时聚合两个值

时间:2018-03-30 15:38:03

标签: sql oracle

编辑:我需要按ID和月分区

我有下表:

TableA1     50       low    201803   20

我想在hoursxWorkingDaysInMonth时多次Cat = 'low'

ID小时猫月工作天数

我有下面的SQL,但它不考虑条件

SELECT SUM(HOURS * MONTH) OVER (PARTITION BY ID, MONTH) AS TESTING,
FROM TABLEA

2 个答案:

答案 0 :(得分:0)

SELECT CASE WHEN cat = 'low' THEN HOURS * WORKINGDAYSINMONTH END AS AMT
FROM TABLEA

SELECT DECODE(CAT,'low',HOURS * WORKINGDAYSINMONTH END) AS AMT
FROM TABLEA

您可以使用CASE或DECODE语句来完成这项工作。

如果您想在其余行中添加默认值,您可以在查询中使用else语句

SELECT CASE WHEN cat = 'low' THEN HOURS * WORKINGDAYSINMONTH 
ELSE 0 END AS AMT
FROM TABLEA

答案 1 :(得分:0)

你似乎想要:

SELECT SUM(A.HOURS * A.WORKINGDAYSINMONTH) AS AMT
FROM TABLEA A
WHERE cat = 'low';

如果你想要每一行的逻辑:

select a.*,
       (case when a.cat = 'low' then a.hour * a.workingdaysinmonth end) as amt
from tablea a;