- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取每个类别总值(value)最高的产品。我按类别获得了正确的“总金额”,但似乎没有带来正确的“产品 ID”和“产品名称”。
SELECT
c.CategoryName,
prodGross.ProductID,
prodGross.ProductName,
MAX(ROUND(Grossed, 2)) AS Grossed
FROM
categories AS c
JOIN
(SELECT
p.ProductID,
p.ProductName,
p.CategoryID,
SUM(((od.UnitPrice * od.Quantity) - ((od.UnitPrice * od.Quantity) * od.Discount))) AS Grossed
FROM
northwind.`order details` AS od
JOIN products AS p ON p.ProductID = od.ProductID
GROUP BY p.ProductID) AS prodGross ON prodGross.CategoryID = c.CategoryID
GROUP BY c.CategoryName;
任何反馈都会有帮助。谢谢!
最佳答案
考虑与 CategoryID 和 Grossed 字段匹配的两个聚合查询的派生表:
SELECT p.*, c.CategoryName, ROUND(g.MaxGrossed) AS HighestGrossed
FROM category c
INNER JOIN
(SELECT subp.ProductID, subp.ProductName, subp.CategoryID,
SUM(((od.UnitPrice * od.Quantity) -
((od.UnitPrice * od.Quantity) * od.Discount))) AS Grossed
FROM northwind.`order details` AS od
INNER JOIN products AS subp ON subp.ProductID = od.ProductID
GROUP BY subp.ProductID) As p
ON c.CategoryID = p.CategoryID
INNER JOIN
(SELECT subg.CategoryID, MAX(subg.Grossed) AS MaxGrossed
FROM
(SELECT subp.ProductID, subp.ProductName, subp.CategoryID,
SUM(((od.UnitPrice * od.Quantity) -
((od.UnitPrice * od.Quantity) * od.Discount))) AS Grossed
FROM northwind.`order details` AS od
INNER JOIN products AS subp ON subp.ProductID = od.ProductID
GROUP BY subp.ProductID) AS subg
GROUP BY subg.CategoryID) AS g
ON p.CategoryID = g.CategoryID AND p.Grossed = g.MaxGrossed
您甚至可以将计算总金额的重复聚合保存为单独的存储 View ,并在此查询中引用它:
SELECT p.*, c.CategoryName, ROUND(g.MaxGrossed) AS HighestGrossed
FROM category c
INNER JOIN ProdGrossedView As p
ON c.CategoryID = p.CategoryID
INNER JOIN
(SELECT v.CategoryID, MAX(v.Grossed) AS MaxGrossed
FROM ProdGrossedView v
GROUP BY v.CategoryID) AS g
ON p.CategoryID = g.CategoryID AND p.Grossed = g.MaxGrossed
关于mysql - 查询未使用 max 函数带来关联值(Northwinds 数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42753071/
当我为我的项目运行“mvn dependency:tree”时,它显示如下: [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ x
我调用 ajax 的脚本 function search_func(value) { $.ajax({ type: "GET", url: "sample.php
我正在使用 SIM 安装 Sitecore 8.1,但在打开启动板时,由于 ajax 请求返回 HTTP 500 错误,出现了几个 Javascript 错误。查看开发人员工具会显示以下消息: htt
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一张 table : +-----------+-------------+------+-----+---------+-------+| Field | Type | N
所以,我对这个阵列很生气,第 2 天让我感到疼痛 *.... 我正在开发面向对象的 PHP 脚本。 我得到一个数组: Array ( [0] => Project Object ( [project_
我正在尝试将我的一个 Rails 项目升级到 Ruby 1.9.2。一切进展顺利,但一个 RSpec 测试失败了。在这个测试中,我需要一个Ruby lib: # file spec/models/my
我可以使用 Maven 的插件 spring-boot:run 运行 Spring Boot,但是当我尝试执行打包的 jar 时,它给出: Error creating bean with name
我是一名优秀的程序员,十分优秀!