SQL calc总订单价格

时间:2015-07-12 20:02:21

标签: sql

我跟随关系,我将在2014年第一季度找到销量最高的前5个订单。我无法弄清楚缺少的是什么

我的解决方案:

SELECT TOP 5 OrderArticle.OrderId as Id,SUM(OrderArticle.Quantity*Article.Price) as Summe
FROM  OrderArticle

JOIN Article ON OrderArticle.ArticleId=Article.Id
JOIN "Order" ON OrderArticle.OrderId='Order'.Id
Group by OrderArticle.OrderId

ORDER BY Summe DESC 

enter image description here

1 个答案:

答案 0 :(得分:0)

您遇到问题,因为Order是保留字。最好的办法是更改表名。您的查询看起来像SQL Server,所以方括号是可行的方法:

SELECT TOP 5 oa.OrderId as Id, SUM(oa.Quantity * a.Price) as Summe
FROM  OrderArticle oa JOIN
      Article a
      ON oa.ArticleId = a.Id JOIN
      [Order] o
      ON oa.OrderId = o.Id
GROUP BY oa.OrderId
ORDER BY Summe DESC ;

仅对字符串和日期常量使用单引号。