简体   繁体   English

计算SQL查询的订单和数量

[英]Calculating orders and quantity for sql query

Good afternoon Stack Overflow community! 下午好,Stack Overflow社区! I'm pretty new to sql and writing queries, so if someone could just double check my work and maybe pass along any helpful tips at how I might go about coding it better? 我对sql和编写查询非常陌生,因此,如果有人可以仔细检查我的工作,并传递有关如何更好地编码的有用提示,我该如何做? I've pasted along the purpose of the query and my code - I appreciate the help and feedback thanks! 我已经粘贴了查询的目的和我的代码-感谢您的帮助和反馈!

Basically the query should create a report of customers living in Europe who have recently placed an order totaling more than $5,000. 基本上,查询应该创建一个报告,该报告涉及居住在欧洲的客户,他们最近下达的订单总额超过5,000美元。 Order total must reflect the quantity of the products the customer ordered, which should also have a discount and order total. 订单总数必须反映客户订购的产品数量,并且还应该有折扣和订单总数。

Thanks again for the help! 再次感谢您的帮助!

select c.CustomerID, c.Country, sum (od.Discount * od.UnitPrice)'Total'
from Customers c
inner join Orders o
on c.CustomerID = o.CustomerID
inner join OrderDetails od
on o.OrderID = od.OrderID
where c.Country in ('germany', 'UK', 'sweden', 'france', 'spain', 'switzerland','austria', 'italy', 'belgium', 'norway', 'denmark', 'finland', 'poland') 
group by c.Country, c.CustomerID
having sum ('Total' * od.Quantity) <= 5000
order by total desc

It looks good, but for a couple of points: 1) Your Total may be wrong 2) You must reverse the comparison operator: 看起来不错,但有两点:1)您的Total可能是错误的2)您必须反转比较运算符:

select c.CustomerID, c.Country, sum (od.Discount * od.UnitPrice * od.Quantity) 'Total'
from Customers c
inner join Orders o
on c.CustomerID = o.CustomerID
inner join OrderDetails od
on o.OrderID = od.OrderID
where c.Country in ('germany', 'UK', 'sweden', 'france', 'spain', 
  'switzerland','austria', 'italy', 'belgium', 'norway', 'denmark', 
  'finland', 'poland') 
and o.OrderDate >= '2017-03-01' -- for example
group by c.Country, c.CustomerID
having sum ( od.Discount * od.UnitPrice * od.Quantity) >= 5000
order by total desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM