0

我已经在这个问题上工作了大约 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

4 回答 4

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)
于 2013-08-19T08:13:47.417 回答
0

你需要分解任务...

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

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

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

我可以为你解决整个问题,但这不会教给你任何东西——这些东西总是很繁琐,只有通过摆弄你才能学会……

于 2013-08-19T08:14:05.630 回答
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      )
于 2013-08-19T08:20:21.163 回答
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
 );
于 2013-08-19T08:21:09.800 回答