2015-07-13 136 views
1

我想獲取一個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。

+3

請提供create table語句,並確保將INSERT添加到table_name子句中並添加語句。而且,你真的需要使用'PL/SQL'嗎?它可以用普通的'SQL'完成。 PL/SQL'LOOP','逐行'又名'slow-by-slow'。 –

+0

更改完成,是的,我需要得到它的PL/SQL ,,代碼準備就緒,我只需要在1輸出下相同的客戶ID的總和 – Shak

+1

什麼是你期待的輸出,爲什麼你「有」在PL/SQL中執行它?請更新您的問題與預期的輸出。 – Boneist

回答

1

你只需要使用組使用SUM作爲聚合函數BY

使用PL/SQL

SQL> SET serveroutput ON 
SQL> DECLARE 
    2 CURSOR cur 
    3 IS 
    4  SELECT SUM(sales.quantity), 
    5  SUM(products.price), 
    6  sales.customer_ID 
    7  FROM sales, 
    8  products 
    9  WHERE sales.product_id=products.product_id 
10  GROUP BY customer_ID 
11  ORDER BY customer_ID DESC; 
12 prod NUMBER; 
13 quan NUMBER(10); 
14 pri NUMBER(10); 
15 c_id NUMBER(10); 
16 BEGIN 
17 OPEN cur; 
18 LOOP 
19  FETCH cur INTO quan,pri,c_id; 
20  EXIT 
21 WHEN cur%notfound; 
22  prod:=pri*quan; 
23  DBMS_OUTPUT.PUT_LINE('customer_id =' || c_id); 
24  DBMS_OUTPUT.PUT_LINE('quantity value =' || quan); 
25  DBMS_OUTPUT.PUT_LINE('price =' || pri); 
26  DBMS_OUTPUT.PUT_LINE('The total value of customer purchases is = ' || prod); 
27 END LOOP; 
28 CLOSE cur; 
29 END; 
30/
customer_id =5 
quantity value =6 
price =621 
The total value of customer purchases is = 3726 
customer_id =4 
quantity value =23 
price =3000 
The total value of customer purchases is = 69000 
customer_id =3 
quantity value =166 
price =3035 
The total value of customer purchases is = 503810 
customer_id =1 
quantity value =688 
price =3121 
The total value of customer purchases is = 2147248 

PL/SQL procedure successfully completed. 

SQL> 

順便說一句,整個PL/SQL塊可以用純SQL。使用SQL

SQL> SELECT SUM(sales.quantity) AS "quantity", 
    2 SUM(products.price) AS "price", 
    3 sales.customer_ID 
    4 FROM sales, 
    5 products 
    6 WHERE sales.product_id=products.product_id 
    7 GROUP BY customer_ID 
    8 ORDER BY customer_ID DESC; 

    quantity  price CUSTOMER_ID 
---------- ---------- ----------- 
     6  621   5 
     23  3000   4 
     166  3035   3 
     688  3121   1 

SQL> 

更新 OP希望過濾基於用戶輸入的行。

的SQL * Plus,你可以聲明一個變量,在過濾謂詞使用它:

SQL> variable cust_id NUMBER 
SQL> EXEC :cust_id:= 4 

PL/SQL procedure successfully completed. 

沒有,讓我們使用上述變量在過濾謂詞:

SQL> SET serveroutput ON 
SQL> DECLARE 
    2 CURSOR cur 
    3 IS 
    4  SELECT SUM(sales.quantity), 
    5  SUM(products.price), 
    6  sales.customer_ID 
    7  FROM sales, 
    8  products 
    9  WHERE sales.product_id=products.product_id 
10  AND sales.customer_ID = :cust_id 
11  GROUP BY customer_ID 
12  ORDER BY customer_ID DESC; 
13 prod NUMBER; 
14 quan NUMBER(10); 
15 pri NUMBER(10); 
16 c_id NUMBER(10); 
17 BEGIN 
18 OPEN cur; 
19 LOOP 
20  FETCH cur INTO quan,pri,c_id; 
21  EXIT 
22 WHEN cur%notfound; 
23  prod:=pri*quan; 
24  DBMS_OUTPUT.PUT_LINE('customer_id =' || c_id); 
25  DBMS_OUTPUT.PUT_LINE('quantity value =' || quan); 
26  DBMS_OUTPUT.PUT_LINE('price =' || pri); 
27  DBMS_OUTPUT.PUT_LINE('The total value of customer purchases is = ' || prod); 
28 END LOOP; 
29 CLOSE cur; 
30 END; 
31/
customer_id =4 
quantity value =23 
price =3000 
The total value of customer purchases is = 69000 

PL/SQL procedure successfully completed. 

SQL> 

如果要從前端應用程序執行此操作,可以將整個邏輯放在PROCEDURE並接受customer_id as IN參數。並在過濾器謂詞中使用它。

+0

非常感謝。最後一個問題,我怎樣才能讓最終用戶輸入客戶ID,輸出的是輸入的ID? – Shak

+0

然後您需要使用過程來擁有INPUT參數。然後在謂詞中使用此輸入參數作爲**綁定變量**來過濾行。或者,在SQL * Plus中,可以使用**替代變量**作爲SQL。 –

+0

@Shak請將其標記爲已回答,以幫助他人。 –

0

您可以使用sum()函數,即select sum(sales.quantity)而不是sales-quantity。這會給你的總數量爲CUSTOMER_ID

2

試試這個

SELECT CUSTOMER_ID 
    ,SUM(QUANTITY) as Total_Quantity 
    ,SUM(QUANTITY*PRICE) as Total_Price 
FROM 
SALES s INNER JOIN 
PRODUCTS p on p.PRODUCT_ID=s.PRODUCT_ID 
GROUP BY CUSTOMER_ID // Group By each customer Id. 
+0

請關於這個問題的意見http://stackoverflow.com/questions/31385608/update-table-pl-sql?noredirect=1#comment50748585_31385608 – Shak

+0

對不起,我是新來的,我不知道你接受答案是什麼意思..謝謝 – Shak

+0

你需要點擊與下面答案相關的「滴答」標記,它會變成綠色,這表明它對你有幫助。所以,下一次,如果有人面臨同樣的問題,那麼他們只是尋找接受的答案。請通過此鏈接瞭解http://stackoverflow.com/tour – Ravi

0
DECLARE 

//聲明變量的

a NUMBER; 
b NUMBER; 
Cal NUMBER; 

PROCEDURE calcul (a IN NUMBER,b  IN NUMBER,Cal OUT NUMBER)IS 

//檢查IF條件如果不等於b,那麼它會增加一個和B BEGIN

 IF a <> b THEN 
      Cal := a + b; 
     END IF; 
    END; 

//傳對a和b的值然後調用calcul過程將做加法。

BEGIN 
    a := 10; 
    b := 15; 
    calcul(a,b,Cal); 
    dbms_output.put_line('Sum is ' || Cal); 
    END; 
+0

請編輯包括一個小的評論,解釋這段代碼如何工作。 –

+0

編輯,謝謝。 –