COUNT 的 SQL MAX(与其他列)
我有一个数据库,其中包含 3 个关系优惠、产品和信息。类别。我正在尝试查找提供的产品数量最多的月份中的哪一天。我需要返回类别名称 (CN)、最大报价数 (OD) 和报价数 (PC) 的日期。
我有这样的疑问:
SELECT CN, MAX(PC)
FROM
(
SELECT c.nombre as CN, EXTRACT(DAY FROM o.fecha) as OD, count(o.id_producto_ofertado) AS PC
FROM ofertas o join (categorias c JOIN productos p ON c.id = p.categoria) on o.id_producto_ofertado = p.id
GROUP BY EXTRACT(DAY FROM o.fecha), c.nombre
)
GROUP BY CN
我想它有效。但结果是与 2 列的关系:CN 和 CN 。最大(电脑)。如果我尝试带上这一天(OD),因为我需要它,通过使用:
SELECT CN, MAX(PC), OD
FROM
(
SELECT c.nombre as CN, EXTRACT(DAY FROM o.fecha) as OD, count(o.id_producto_ofertado) AS PC
FROM ofertas o join (categorias c JOIN productos p ON c.id = p.categoria) on o.id_producto_ofertado = p.id
GROUP BY EXTRACT(DAY FROM o.fecha), c.nombre
)
GROUP BY CN, OD
然后它返回一个 3 列关系: CN, MAX(PC) &外径。但由于类别名称 (CN) 重复,我认为这是因为我需要按 OD 进行分组才能提取日期。因此,返回的 31 天中每一天都包含一行。
我只需要每个类别名称一行,返回类别名称、报价数量最多的月份中的第几天和报价数量最多的月份。
我现在被困住了,因为我不知道该怎么办。谢谢
I have a database with 3 relations Offers, Products & Categories. I'm trying to find the day of the month with the max number of offered products. I need to return the Category Name (CN), Day with the max number of offers (OD) and the number of offers (PC).
I have this query:
SELECT CN, MAX(PC)
FROM
(
SELECT c.nombre as CN, EXTRACT(DAY FROM o.fecha) as OD, count(o.id_producto_ofertado) AS PC
FROM ofertas o join (categorias c JOIN productos p ON c.id = p.categoria) on o.id_producto_ofertado = p.id
GROUP BY EXTRACT(DAY FROM o.fecha), c.nombre
)
GROUP BY CN
I suppose it works. But the result is a relation with 2 cols: CN & MAX(PC). If I try to bring the day (OD), because I need it, by using:
SELECT CN, MAX(PC), OD
FROM
(
SELECT c.nombre as CN, EXTRACT(DAY FROM o.fecha) as OD, count(o.id_producto_ofertado) AS PC
FROM ofertas o join (categorias c JOIN productos p ON c.id = p.categoria) on o.id_producto_ofertado = p.id
GROUP BY EXTRACT(DAY FROM o.fecha), c.nombre
)
GROUP BY CN, OD
Then it return a 3 cols relation: CN, MAX(PC) & OD. BUT with the Category Name (CN) repeated, I think that because I need to group by OD in order to extract also the day. So, the return contains a row for each of the 31 days.
I just need a row for each category name, returning the category name, the number of the day of the month with the greatest number of offers and the greatest number of offers.
I'm stuck right now, cause I don't know what to do. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
row_number
选择报价计数最高的行:row_number
的子查询可能不是必需的,但我附近没有测试 Oracle。You could use
row_number
to select the row with the highest offer count:The subquery for
row_number
might not be necessary, but I haven't got a test Oracle nearby.