我正在运行此查询来测试我的功能
select first_name, last_name,get_balance_due(orders.order_id)
from customers, orders, order_lines
Where customers.customer_id=orders.customer_id and orders.order_id = order_lines.order_id and
order_date >= to_date( '30-Nov-1999', 'DD-Mon-YYYY' )
AND order_date< to_date( '01-Dec-1999', 'DD-Mon-YYYY' );
Group By First_name, Last_name, get_balance_due(orders.order_id)
输出重复3次。
FIRST_NAME LAST_NAME GET_BALANCE_DUE(ORDERS.ORDER_ID)
Jan Busse 602450.56
Jan Busse 602450.56
Jan Busse 602450.56
函数get_balance_due
CREATE OR REPLACE FUNCTION Get_balance_due
(order_id_f NUMBER)
RETURN NUMBER
AS
balance_due_f NUMBER;
BEGIN
SELECT SUM(total_amount) INTO balance_due_f
FROM order_lines
WHERE order_id_f = order_id
RETURN balance_due_f;
end;
我怎样才能得到1个结果而不是3?
答案 0 :(得分:1)
从select语句中删除order_lines:
select first_name, last_name,get_balance_due(orders.order_id)
from customers INNER JOIN orders
ON customers.customer_id=orders.customer_id
Where
order_date >= to_date( '30-Nov-1999', 'DD-Mon-YYYY' )
AND order_date< to_date( '01-Dec-1999', 'DD-Mon-YYYY' );
(并使用INNER JOINs)