2014-04-16 115 views
0

我試圖顯示每個客戶的訂單數作爲dbms_output.put_line但計數不正確...我做錯了什麼?找到每個客戶的訂單數

declare 
    t_order  number; 
    cust_id  customer.c_id%type; 
    orders_cid orders.c_id%type; 
    cust_first customer.c_first%type; 
    cust_last customer.c_last%type; 

    cursor c is 
    select distinct(customer.c_id), 
      customer.c_first, 
      customer.c_last, 
      orders.c_id 
     from customer 
     join orders 
     on customer.c_id = orders.c_id; 
begin 
    dbms_output.put_line('AZ SPORTS OUTSTANDING ORDERS'); 
    dbms_output.put_line('FIRST LAST TOTAL'); 
    dbms_output.put_line('NAME NAME ORDERS'); 

    OPEN C; 
    LOOP 
    FETCH C INTO CUST_ID, CUST_FIRST, CUST_LAST, ORDERS_CID; 
    EXIT WHEN C%NOTFOUND; 

    IF CUST_ID=ORDERS_CID THEN 
     select count(distinct(orders.c_id)) 
     into t_order 
     from orders 
     join customer 
      on orders.c_id = customer.c_id 

     DBMS_OUTPUT.PUT_LINE(TO_CHAR(CUST_FIRST) || ' ' || 
          TO_CHAR(CUST_LAST) || ' ' || 
          TO_CHAR(T_ORDER)); 
    END IF; 
    END LOOP; 

    CLOSE C; 
END; 
/

每位顧客的訂單數量顯示爲5,一直到每位顧客的訂單數量都不是正確的答案。

回答

0

在我看來,你需要SELECT COUNT...聲明中的WHERE子句 - 可能類似於WHERE ORDERS.C_ID = ORDERS_CID AND CUSTOMER.C_ID = CUST_ID。試試看看它是如何工作的。

分享和享受。

+0

niceee感謝它的工作原理:D ty太多了! – Jongu

相關問題