Я использую базу данных Northwind, и для каждого региона мне нужно показать клиенту, у которого был самый высокий объем заказа
Мои таблицы:
-- Customers --
[CustomerID] [nchar](5) NOT NULL,
[CompanyName] [nvarchar](40) NOT NULL,
[ContactName] [nvarchar](30) NULL,
[ContactTitle] [nvarchar](30) NULL,
[Address] [nvarchar](60) NULL,
[City] [nvarchar](15) NULL,
[Region] [nvarchar](15) NULL,
[PostalCode] [nvarchar](10) NULL,
[Country] [nvarchar](15) NULL,
[Phone] [nvarchar](24) NULL,
[Fax] [nvarchar](24) NULL,
--OrderDetails
[OrderID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
[UnitPrice] [money] NOT NULL,
[Quantity] [smallint] NOT NULL,
[Discount] [real] NOT NULL,
CONSTRAINT [PK_Order_Details] PRIMARY KEY CLUSTERED
-- Orders
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] [nchar](5) NOT NULL,
[EmployeeID] [int] NULL,
[OrderDate] [datetime] NULL,
[RequiredDate] [datetime] NULL,
[ShippedDate] [datetime] NULL,
[ShipVia] [int] NULL,
[Freight] [money] NULL,
[ShipName] [nvarchar](40) NULL,
[ShipAddress] [nvarchar](60) NULL,
[ShipCity] [nvarchar](15) NULL,
[ShipRegion] [nvarchar](15) NULL,
[ShipPostalCode] [nvarchar](10) NULL,
[ShipCountry] [nvarchar](15) NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
Я начинаю спытаюсь подсчитать для каждого клиента его общую сумму
select
sum(unitprice * quantity * 1+Discount) as ValueOfOrders
from
OrderDetails od
join
orders o on od.OrderID = o.OrderID
group by
CustomerID
Но я не знаю, как использовать функцию max
, чтобы связать ее с регионом.