我正在尝试获取PLSQL语句以显示所有客户ID列表以及每个客户的订单值总和。下面的代码得到了正确的答案,但问题是我有多个客户具有相同的ID,我需要在1 ID输出下汇总所有订单值而不是multiple.my代码为同一客户获取多个输出。
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
请adivse。
答案 0 :(得分: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.
答案 1 :(得分:1)
您只需使用 SUM 作为聚合函数 GROUP 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
作为 IN参数 。并在过滤谓词中使用它。
答案 2 :(得分:0)
您可以使用sum()函数,例如select sum(sales.quantity)而不是sales-quantity。这将为您提供该customer_id的总数量
答案 3 :(得分:0)
DECLARE
//声明变量
a NUMBER;
b NUMBER;
Cal NUMBER;
PROCEDURE calcul (a IN NUMBER,b IN NUMBER,Cal OUT NUMBER)IS
//检查IF条件是否等于b,那么它将添加a和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;