我需要在>时提取数据的最大值。 0和最小数据时< 0.棘手的部分是它应该在同一列中。示例如下: 查询下方:
SELECT A.employee_id, paycheck_number
max ( CASE
WHEN B.special = 'XXT' THEN B.AMOUNT
ELSE 0
END ) AMOUNT,
max ( CASE
WHEN B.special = 'XXH' THEN B.hrs1
ELSE 0
END ) HRS1
FROM Table1 A,
table2 B
LEFT OUTER JOIN table3 C
ON B.company = C.company
WHERE A .employee_id = '123456789'
GROUP BY A.employee_id, paycheck_number
ORDER BY 1
返回:
EMPLOYEE_ID AMOUNT HRS1 paycheck_number 123456789 2799.82 134.84 1234 123456789 832.86 40 4321 123456789 0 0 5678
如果按数据删除该组,则为:
EMPLOYEE_ID AMOUNT HRS1 paycheck_number 123456789 0 134.84 1234 123456789 2799.82 0 1234 123456789 0 40 4321 123456789 832.86 0 4321 123456789 0 -40 5678 123456789 -832.86 0 5678
我想要:
EMPLOYEE_ID AMOUNT HRS1 paycheck_number 123456789 2799.82 134.84 1234 123456789 832.86 40 4321 123456789 -832.86 -40 5678
看起来很简单,但是当我尝试时它不起作用。
答案 0 :(得分:1)
In Oracle you get the value for the maximum absolute amount via KEEP DENSE_RANK FIRST/LAST
:
max(value) keep (dense_rank last order by abs(value))
However, when there is always only the one non-zero value in one record and zeros or nulls in the other records to consider, you could simply add them:
sum(value)
In your case where only one record actually contains a value, you are creating the zero entries yourself and thus get 0 instead of, say, -40 when asking for the maximum value. Remove the ELSE
branches that create the interfering zeros, and MAX
will get you the correct value. E.g.:
max(case when b.special = 'XXT' then b.amount end) as amount