在下面的查询中,我试图制作一个快速摘要表来显示一些信息。使用where子句意味着从数据库中查找与该发布者相关的信息。在查看了一个小时左右的其他定义之后,我无法看到我出错的地方。
很抱歉这是一个痛苦,这可能是一件非常简单的事情,但我在下面的查询中有任何明显的错误。
SELECT Publisher.Name AS "Publisher Name",
Book.Title AS "Book Title",
ShopOrder.OrderDate AS "Order Date",
OrderLine.Quantity AS "Order Quantity",
(SUM(Quantity*UnitSellingPrice)) AS "Total Value"
FROM ShopOrder
INNER JOIN OrderLine
ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID
INNER JOIN Book
ON OrderLine.BookID=Book.BookID
INNER JOIN Publisher
ON Publisher.PublisherID=Book.PublisherID
WHERE Publisher.Name='Oxford'
GROUP BY ShopOrder.ShopOrderID,
Publisher.PublisherID,
Book.BookID,
OrderLine.Quantity
ORDER BY ShopOrder.OrderDate;
非常感谢任何可以提供帮助的人!
编辑: 相关表格定义
CREATE TABLE Publisher
(
PublisherID INTEGER,
Name VARCHAR(50),
PRIMARY KEY(PublisherID)
);
CREATE TABLE Book
(
BookID INTEGER,
Title VARCHAR(50),
Price DECIMAL (10, 2),
CategoryID INTEGER REFERENCES Category(CategoryID),
PublisherID INTEGER REFERENCES Publisher(PublisherID),
PRIMARY KEY(BookID)
);
CREATE TABLE ShopOrder
(
ShopOrderID INTEGER,
OrderDate DATE,
ShopID INTEGER REFERENCES Shop(ShopID),
SalesRepID INTEGER REFERENCES SalesRep(SalesRepID),
PRIMARY KEY(ShopOrderID)
);
CREATE TABLE OrderLine
(
ShopOrderID INTEGER REFERENCES ShopOrder(ShopOrderID) ON DELETE CASCADE,
BookID INTEGER REFERENCES Book(BookID),
Quantity INTEGER,
UnitSellingPrice DECIMAL (10,2)
);
答案 0 :(得分:1)
这不是一个明确的答案,但我已添加它以显示 I 将如何调试查询:
从Publisher
开始:
SELECT Publisher.Name AS "Publisher Name"
FROM Publisher
WHERE Publisher.Name='Oxford'
检查结果......
添加Book
SELECT Publisher.Name AS "Publisher Name",
Book.Title AS "Book Title"
FROM Book
INNER JOIN Publisher
ON Publisher.PublisherID=Book.PublisherID
WHERE Publisher.Name='Oxford'
检查结果......
添加OrderLine
:
SELECT Publisher.Name AS "Publisher Name",
Book.Title AS "Book Title",
OrderLine.Quantity AS "Order Quantity"
FROM OrderLine
INNER JOIN Book
ON OrderLine.BookID=Book.BookID
INNER JOIN Publisher
ON Publisher.PublisherID=Book.PublisherID
WHERE Publisher.Name='Oxford'
检查结果......
添加ShopOrder
SELECT Publisher.Name AS "Publisher Name",
Book.Title AS "Book Title",
ShopOrder.OrderDate AS "Order Date",
OrderLine.Quantity AS "Order Quantity"
FROM ShopOrder
INNER JOIN OrderLine
ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID
INNER JOIN Book
ON OrderLine.BookID=Book.BookID
INNER JOIN Publisher
ON Publisher.PublisherID=Book.PublisherID
WHERE Publisher.Name='Oxford'
检查结果......
添加SUM和GROUPING:
SELECT Publisher.Name AS "Publisher Name",
Book.Title AS "Book Title",
ShopOrder.OrderDate AS "Order Date",
OrderLine.Quantity AS "Order Quantity",
(SUM(Quantity*UnitSellingPrice)) AS "Total Value"
FROM ShopOrder
INNER JOIN OrderLine
ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID
INNER JOIN Book
ON OrderLine.BookID=Book.BookID
INNER JOIN Publisher
ON Publisher.PublisherID=Book.PublisherID
WHERE Publisher.Name='Oxford'
GROUP BY ShopOrder.ShopOrderID,
Publisher.PublisherID,
Book.BookID,
OrderLine.Quantity
ORDER BY ShopOrder.OrderDate;
答案 1 :(得分:0)
Stanleys的回答是在这种情况下真正有用的,但对于任何人来说,在某种程度上寻找答案,正确的查询应如下所示。
SELECT Publisher.Name AS "Publisher Name", Book.Title AS "Book Title", ShopOrder.OrderDate AS "Order Date",
OrderLine.Quantity AS "Order Quantity", (SUM(Quantity*UnitSellingPrice)) AS "Total Value"
FROM ShopOrder
INNER JOIN OrderLine
ON ShopOrder.ShopOrderID=OrderLine.ShopOrderID
INNER JOIN Book
ON OrderLine.BookID=Book.BookID
INNER JOIN Publisher
ON Publisher.PublisherID=Book.BookID
WHERE Publisher.Name='Oxford'
GROUP BY publisher.name, book.title, shoporder.orderdate, orderline.quantity
ORDER BY ShopOrder.OrderDate;