的問題是,你有多個客戶在customer1
表的'Nick Rimando'
的cust_name
。看看
select count(1)
from customer1
where cust_name = 'Nick Rimando';
我敢打賭,這將導致數大於1
的修復(ES)
的修復取決於你的實際需要。而你其實並沒有寫出你需要的東西。因此,少數病例來我的心......
案例1 - 任意單個客戶的購買與總和 - 所有客戶的購買與該名稱
create or replace procedure discount_purchase(cust_name customer1.cust_name%type)
as
amt int;
discount int;
begin
select sum(purch_amt) into amt
from orders
where customer_id in (select customer_id from customer1 where cust_name=cust_name);
dbms_output.put_line('Total amount is='||amt);
if(amt>=500) then
discount:=amt-(0.25*amt);
dbms_output.put_line('Discount amount is='||discount);
else
dbms_output.put_line('NO Discount on='||amt||'Discount only above 500');
end if;
end;
/
案例2的總和命名
create or replace procedure discount_purchase(cust_name customer1.cust_name%type)
as
amt int;
discount int;
begin
select sum(purch_amt) into amt
from orders
where customer_id = (select customer_id from customer1 where cust_name=cust_name and rownum <= 1);
dbms_output.put_line('Total amount is='||amt);
if(amt>=500) then
discount:=amt-(0.25*amt);
dbms_output.put_line('Discount amount is='||discount);
else
dbms_output.put_line('NO Discount on='||amt||'Discount only above 500');
end if;
end;
/
案例3 - 「第一個」 客戶購買的總和與該名稱
create or replace procedure discount_purchase(cust_name customer1.cust_name%type)
as
amt int;
discount int;
begin
select sum(purch_amt) into amt
from orders
where customer_id = (select min(customer_id) from customer1 where cust_name=cust_name);
dbms_output.put_line('Total amount is='||amt);
if(amt>=500) then
discount:=amt-(0.25*amt);
dbms_output.put_line('Discount amount is='||discount);
else
dbms_output.put_line('NO Discount on='||amt||'Discount only above 500');
end if;
end;
/
案例4 - 你選擇,你實現
我不能告訴你怎麼樣,那是完全取決於你和你的設計考慮。
案例5 - 修復你的數據模型
同樣,我不能告訴你怎麼樣,那是完全取決於你和你的設計考慮。
使用別名並獲得第一行子查詢 –
子查詢返回單個值3002作爲customer_id –
如果您不關心列表中的值或者確定它們是否爲您的子查詢條件,那麼嘗試添加rownum = 1和rownum = 1一樣。 –