使用Oracle的子查询

时间:2013-08-19 08:02:53

标签: oracle

我一直在研究这个问题大约8个小时但没有成功。有没有人有任何想法?

问题: 使用正确的表格,使用您希望列出客户编号的连接操作,连接在一起的名字和姓氏以及为订购最昂贵的图书的所有客户(基于零售价格)创建一个子查询。给出组合的客户名称列和别名“客户名称”。

这是我的代码:

SELECT book_customer.customerid, lastname || ', ' || firstname AS "Customer Name", city
FROM book_customer, book_order, order_items 
    WHERE book_customer.customerid = book_order.customerid
        AND book_order.orderid = order_items.orderid
            AND bookid =
            (
            SELECT MAX(retail) 
                FROM books 
                    GROUP BY bookid
            )
;

结果:

 SQL> SELECT book_customer.customerid, lastname || ', ' || firstname AS "Customer Name", city
  2   FROM book_customer, book_order, order_items 
  3    WHERE book_customer.customerid = book_order.customerid
  4     AND book_order.orderid = order_items.orderid
  5      AND bookid =
  6      (
  7      SELECT MAX(retail) 
  8       FROM books 
  9        GROUP BY bookid
 10      )
 11  ;
    SELECT MAX(retail)
    *
ERROR at line 7:
ORA-01427: single-row subquery returns more than one row

我也试过这个:

SELECT customerid, lastname || ', ' || fistname AS "Customer Name", city
FROM book_customer, book_order
    WHERE book_customer.customerid = book_order.customerid
        AND orderid = 
        (
        SELECT orderid
            FROM order_items
                AND bookid =
                 (
                 SELECT bookin
                    FROM books
                        GROUP BY bookid
                            HAVING MAX(retail)
                  )
        )
;

结果:

  SQL> SELECT customerid, lastname || ', ' || fistname AS "Customer Name", city
  2   FROM book_customer, book_order
  3    WHERE book_customer.customerid = book_order.customerid
  4      AND orderid = 
  5      (
  6      SELECT orderid
  7       FROM order_items
  8         AND bookid =
  9          (
 10          SELECT bookid
 11            FROM books
 12              GROUP BY bookid
 13                HAVING MAX(retail)
 14           )
 15      )
 16  ;
       AND bookid =
       *
ERROR at line 8:
ORA-00907: missing right parenthesis

ERD

Book_Customer TABLE

CustomerID              PK
Lastname
Firstname
Address
City
State
Zip
Referred

Book_Order TABLE

OrderID               PK
CustomerID            FK
OrderDate
ShipDate
ShipStreet
ShipCity
ShipState
ShipZip

Order_Items TABLE

OrderID               PK/FK
ItemNum               PK/FK
BookID
Quantity

Books TABLE

BookID                PK
ISBN 
Title
PubDate
PubID                 FK
Cost
Retail
Category

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
Book_Customer.CustomerID, 
Book_Customer.lastname || ', ' || Book_Customer.firstname AS "Customer Name", 
Book_Customer.city
FROM Order_Items 
JOIN Book_Order ON Order_Items.OrderID = Book_Order.OrderID
JOIN Book_Customer ON Book_Customer.CustomerID = Book_Order.CustomerID
JOIN Books ON Books.BookID = Order_Items.BookID
WHERE Books.Retail = (SELECT MAX(Retail) FROM Books)

答案 1 :(得分:0)

你需要打破任务......

第一步是确定您正在处理哪本书 - 如下所示:

Select BookID from books where RowNum=1 order by retail desc

这基本上成为您的子查询,然后您可以在联接中使用它来收集所需的数据。

我可以为你解决整个问题,但这不会教会你什么 - 这些东西总是很繁琐,只有通过摆弄才能学会......

答案 2 :(得分:0)

对于第一个错误请尝试以下查询

 SQL> SELECT bc.customerid, lastname || ', ' || firstname AS "Customer Name", city
2   FROM book_customer bc, book_order bo, order_items  oi
3    WHERE book_customer.customerid = book_order.customerid
4     AND book_order.orderid = order_items.orderid
5      AND bookid =
6       (
 7      SELECT MAX(retail) 
 8       FROM books 
 9        GROUP BY bookid
10      )

请在查询中提供tablename.column名称,因为查询中使用的多个表中可以存在相同的列。我可以看到CustomerId存在于多个表中。你也可以使用别名。就像我上面做的那样

对于第二个问题,我提供了有错误的评论: -

SELECT customerid, lastname || ', ' || fistname AS "Customer Name", city
2   FROM book_customer, book_order
 3    WHERE book_customer.customerid = book_order.customerid
 4      AND orderid = 
 5      (
 6      SELECT orderid
7       FROM order_items
8         AND bookid =--no where caluse here it should be WHERE bookid=
9          (
10          SELECT bookin
 11            FROM books
 12              GROUP BY bookid
 13                HAVING MAX(retail)
   14           )
 15      )

答案 3 :(得分:0)

试试这个,

 SELECT a.customerid, a.lastname || ', ' || a.firstname AS Customer_Name, a.city
   FROM book_customer a, 
        book_order b, 
        order_items c 
  WHERE a.customerid = b.customerid
   AND b.orderid = c.orderid
   AND c.bookid =
 (
      SELECT MAX(retail) 
        FROM books 
      GROUP BY bookid
 );