我创建了表名customers
orders
,orderdetails
如下所示
CREATE TABLE `customers` (
`customerNumber` int(11) NOT NULL,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(50) NOT NULL,
`contactFirstName` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`addressLine1` varchar(50) NOT NULL,
`addressLine2` varchar(50) default NULL,
`city` varchar(50) NOT NULL,
`state` varchar(50) default NULL,
`postalCode` varchar(15) default NULL,
`country` varchar(50) NOT NULL,
`salesRepEmployeeNumber` int(11) default NULL,
`creditLimit` double default NULL,
PRIMARY KEY (`customerNumber`)
)
CREATE TABLE `orders` (
`orderNumber` int(11) NOT NULL,
`orderDate` datetime NOT NULL,
`requiredDate` datetime NOT NULL,
`shippedDate` datetime default NULL,
`status` varchar(15) NOT NULL,
`comments` text,
`customerNumber` int(11) NOT NULL,
PRIMARY KEY (`orderNumber`)
)
CREATE TABLE `orderdetails` (
`orderNumber` int(11) NOT NULL,
`productCode` varchar(15) NOT NULL,
`quantityOrdered` int(11) NOT NULL,
`priceEach` double NOT NULL,
`orderLineNumber` smallint(6) NOT NULL,
PRIMARY KEY (`orderNumber`,`productCode`)
)
之后,我必须找出每个城市的订单最多的客户。我计算了每个客户的总订单量,如下所示,但我不知道如何从每个城市中选择最大订单的客户。所以问题是我怎么能做到这一点。
SELECT o.customerNumber, ROUND( SUM( od.priceEach * od.quantityOrdered ) , 2 ) AS totalOrder
FROM orders AS o
INNER JOIN orderDetails AS od ON o.orderNumber = od.orderNumber
GROUP BY o.customerNumber
ORDER BY totalOrder DESC
我正在使用mysql。