COUNT 的 SQL MAX(与其他列)

发布于 2024-10-18 04:51:13 字数 1011 浏览 4 评论 0原文

我有一个数据库,其中包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2024-10-25 04:51:13

您可以使用 row_number 选择报价计数最高的行:

select  CustomerName
,       OrderDate
,       OfferCount
from    (
        select  CustomerName
        ,       OrderDate
        ,       OfferCount
        ,       row_number() over (partition by CustomerName
                                   order by OfferCount desc) as rn
        from    (
                select  c.nombre as CustomerName
                ,       EXTRACT(DAY FROM o.fecha) as OrderDate
                ,       count(o.id_producto_ofertado) as OfferCount
                FROM    ofertas o 
                join    categorias c 
                on      o.id_producto_ofertado = p.id
                join    productos p 
                on      c.id = p.categoria
                group by
                        c.nombre
                ,       EXTRACT(DAY FROM o.fecha)
                ) as GroupByDay
        ) as WithRowNumber
where   rn = 1

row_number 的子查询可能不是必需的,但我附近没有测试 Oracle。

You could use row_number to select the row with the highest offer count:

select  CustomerName
,       OrderDate
,       OfferCount
from    (
        select  CustomerName
        ,       OrderDate
        ,       OfferCount
        ,       row_number() over (partition by CustomerName
                                   order by OfferCount desc) as rn
        from    (
                select  c.nombre as CustomerName
                ,       EXTRACT(DAY FROM o.fecha) as OrderDate
                ,       count(o.id_producto_ofertado) as OfferCount
                FROM    ofertas o 
                join    categorias c 
                on      o.id_producto_ofertado = p.id
                join    productos p 
                on      c.id = p.categoria
                group by
                        c.nombre
                ,       EXTRACT(DAY FROM o.fecha)
                ) as GroupByDay
        ) as WithRowNumber
where   rn = 1

The subquery for row_number might not be necessary, but I haven't got a test Oracle nearby.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文