gpt4 book ai didi

mysql - 如何构建 SQL 来查找每个产品在不同日期和时间的最大产品数?

转载 作者:行者123 更新时间:2023-11-29 16:33:59 25 4
gpt4 key购买 nike

我需要一个 SQL 来查找每个产品的 QNTT 最大值并了解日期和时间。

+-----------+--------+-----------+-----------+
| Product | QNTT | date | hour |
+-----------+--------+-----------+-----------+
| AAA | 31 | 28-nov-18 | 09 |
| AAA | 21 | 28-nov-18 | 10 |
| AAA | 52 | 28-nov-18 | 11 |
| AAA | 11 | 28-nov-18 | 12 |
| AAA | 15 | 29-nov-18 | 09 |
| AAA | 19 | 29-nov-18 | 10 |
| AAA | 22 | 29-nov-18 | 11 |
| AAA | 33 | 29-nov-18 | 12 |
| AAA | 55 | 30-nov-18 | 09 |
| AAA | 42 | 30-nov-18 | 10 |
| AAA | 31 | 30-nov-18 | 11 |
| AAA | 21 | 30-nov-18 | 12 |
| BBB | 31 | 28-nov-18 | 09 |
| BBB | 21 | 28-nov-18 | 10 |
| BBB | 12 | 28-nov-18 | 11 |
| BBB | 58 | 28-nov-18 | 12 |
| BBB | 15 | 29-nov-18 | 09 |
| BBB | 19 | 29-nov-18 | 10 |
| BBB | 22 | 29-nov-18 | 11 |
| BBB | 33 | 29-nov-18 | 12 |
| BBB | 43 | 30-nov-18 | 09 |
| BBB | 42 | 30-nov-18 | 10 |
| BBB | 51 | 30-nov-18 | 11 |
| BBB | 21 | 30-nov-18 | 12 |
+-----------+--------+-----------+-----------+

我想要的 SQL 答案都是 MAX(QNTT) 的产品:

+-----------+--------+-----------+-----------+
| Product | QNTT | date | hour |
+-----------+--------+-----------+-----------+
| AAA | 55 | 30-nov-18 | 09 |
| BBB | 58 | 28-nov-18 | 12 |
+-----------+--------+-----------+-----------+

我可以找到没有时间和日期的结果:

SELECT
Product, max(QNTT)
FROM
TABLE
WHERE
Month(DATE) = 11 AND YEAR(DATE) = 2018
AND
Product <> 'AAAB'
AND
Substr(Product, 1, 3) IN ('AAA','BBB')
AND
hour<= 18
AND
Day(DATE) > 13
GROUP BY
Product
ORDER BY
Product, QNTT DESC

但我需要日期和时间才能知道该月每个时间的最大数量。

最佳答案

您可以将子查询group by Product一起使用,如下所示:

select *
from tab
where ( Product, QNTT ) in
(select Product, max(QNTT)
from tab
group by Product ) t

关于mysql - 如何构建 SQL 来查找每个产品在不同日期和时间的最大产品数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53728983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com