我需要编写一个查询来计算与给定条件匹配的字段值的总和。所以,我写了这个自定义报告功能
Function get_order_amount (ord_amount as number, inv_date as date, month as string, year as string)
-- ...
if inv_date(yyyymm) <= tonumber(year + month) then
return ord_amount
else
return 0
end if
End Function
现在,我希望在命令查询中使用我的函数来过滤数据总和。即: (q_sum)
SELECT EXTRACT(YEAR FROM ORDER_DATE) AS "YEAR",
SUM(GET_ORDER_AMOUNT(AMOUNT,INVOICE_DATE,{?P_MONTH},{?P_YEAR}))
FROM ORDERS
GROUP BY EXTRACT(YEAR FROM ORDER_DATE)
我怎样才能做到这一点?
答案 0 :(得分:0)
我不相信您可以将自定义函数与SQL查询混合使用。
相反,您可以直接在SQL中使用CASE语句。
案例陈述看起来像这样
Select EXTRACT(YEAR FROM ORDER_DATE) as YEAR
, SUM(CASE
WHEN INV_DATE <= **SOMEDATE** THEN
ORD_AMOUNT
ELSE
0
END) as Custom_Total
FROM ORDERS
GROUP BY EXTRACT(YEAR FROM ORDER_DATE)