我使用NorthWind数据库,我的目的是在一行中选择每个城市的发货订单总数。对不起虚拟问题,但我是Sql新手
select e.FirstName , o.ShipCity, od.UnitPrice,Sum(od.Quantity),
sum(od.Quantity*od.UnitPrice) from [Order Details] as od
inner join Products as p on p.ProductID=od.ProductID
inner join Orders as o on o.OrderID =od.OrderID
inner join Employees as e on e.EmployeeID=o.EmployeeID
where o.ShipCity= 'Graz' and o.isCanceled !=1 and e.FirstName ='Nancy'
group by e.FirstName , o.ShipCity,od.UnitPrice
答案 0 :(得分:1)
=>
select e.firstName , o.ShipCity , sum ( od.Quantity) from [ order Details ] as od
inner join Orders as o on o.OrderId = od.OrderId
inner join Employees as e on e.employeeID=o.EmployeeID
where o.shipcity = 'Graz'and o.iscanceled !=1 and e.firstname='Nancy'
group by e.firstName , o.ShipCity
答案 1 :(得分:0)
要获取每个城市的金额,您必须从选择中删除FirstName
和UnitPrice
。
select o.ShipCity,
Sum(od.Quantity),
sum(od.Quantity*od.UnitPrice)
from [Order Details] as od
inner join Products as p on p.ProductID=od.ProductID
inner join Orders as o on o.OrderID =od.OrderID
inner join Employees as e on e.EmployeeID=o.EmployeeID
where o.ShipCity= 'Graz' and o.isCanceled !=1 and e.FirstName ='Nancy'
group by o.ShipCity