Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 查找每个客户的订单数_Oracle_Plsql_Cursor - Fatal编程技术网

Oracle 查找每个客户的订单数

Oracle 查找每个客户的订单数,oracle,plsql,cursor,Oracle,Plsql,Cursor,我试图将每个客户的订单数量显示为dbms_输出。put_行,但计数不正确…我做错了什么 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 disti

我试图将每个客户的订单数量显示为dbms_输出。put_行,但计数不正确…我做错了什么

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,这不是正确答案。

在我看来,您需要在
选择计数…
语句中使用WHERE子句-可能类似于
WHERE orders.C\u ID=orders\u CID和customer.C\u ID=CUST\u ID
。试试看它是如何工作的

分享和享受