我想獲取一個PLSQL語句來顯示客戶ID的所有列表和每個客戶的訂單價值總和。下面的代碼讓我得到了正確的答案,但問題是我擁有多個具有相同ID的客戶,並且我只需要在1個ID輸出下彙總所有訂單值,而不是multiple.my代碼爲同一個客戶提供多個輸出。總價值總和PL/Sql
Create table sales (customer_ID number(10), product_ID number(10), quantity number(10));
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,1,23);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(1,2,34);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(1,3,654);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,7,32);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(4,3,23);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,3,111);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(5,4,6);
Create table products (product_ID number(10), price number(10));
INSERT INTO products (product_ID, price) Values(1,32);
INSERT INTO products (product_ID, price) Values(2,121);
INSERT INTO products (product_ID, price) Values(3,3000);
INSERT INTO products (product_ID, price) Values(4,621);
INSERT INTO products (product_ID, price) Values(5,363);
INSERT INTO products (product_ID, price) Values(6,32);
INSERT INTO products (product_ID, price) Values(7,3);
INSERT INTO products (product_ID, price) Values(8,432);
INSERT INTO products (product_ID, price) Values(9,11);
INSERT INTO products (product_ID, price) Values(10,73);
declare
cursor cur is select unique sales.quantity,products.price,sales.customer_ID
from sales,products
where sales.product_id=products.product_id
order by customer_ID desc;
prod number;
quan number(10);
pri number(10);
c_id number(10);
begin
open cur;
loop
fetch cur into quan,pri,c_id;
exit when cur%notfound;
prod:=pri*quan;
DBMS_OUTPUT.PUT_LINE('customer_id =' || c_id);
DBMS_OUTPUT.PUT_LINE('quantity value =' || quan);
DBMS_OUTPUT.PUT_LINE('price =' || pri);
DBMS_OUTPUT.PUT_LINE('The total value of customer purchases is = ' || prod);
end loop;
close cur;
END
please adivse。
請提供create table語句,並確保將INSERT添加到table_name子句中並添加語句。而且,你真的需要使用'PL/SQL'嗎?它可以用普通的'SQL'完成。 PL/SQL'LOOP','逐行'又名'slow-by-slow'。 –
更改完成,是的,我需要得到它的PL/SQL ,,代碼準備就緒,我只需要在1輸出下相同的客戶ID的總和 – Shak
什麼是你期待的輸出,爲什麼你「有」在PL/SQL中執行它?請更新您的問題與預期的輸出。 – Boneist