如何查询仅具有最高值的所有不同行?

How to do I query all distinct rows with only their highest values?

我一直在尝试查询每个城市的流行类型。我只是想获取我突出显示的行。我尝试在 group by 上使用 MAX(),但出现语法错误。

我的 CTE 查询如下,它基于 dbeaver 示例数据集:

with q_table
as 
(   select City, Genre, count(*) as counts
    from 
        (select c.City, g.Name as Genre
        from bus5dwr.dbeaver_sample.Customer c
        inner join bus5dwr.dbeaver_sample.Invoice i
            on i.CustomerId = c.CustomerId
        inner join bus5dwr.dbeaver_sample.InvoiceLine il
            on il.InvoiceId = i.InvoiceId 
        inner join bus5dwr.dbeaver_sample.track t
            on t.TrackId = il.TrackId 
        inner join bus5dwr.dbeaver_sample.Genre g
            on g.GenreId = t.GenreId 
        where Country = 'USA'
        ) as t2
    group by City, Genre)

我尝试了以下查询。

我没有可对其进行测试的数据集,但您应该能够向您的 CTE 添加一个 ROW_NUMBER() 函数来获取您正在寻找的值。如:

with q_table
as 
(   select City, Genre, count(*) as counts,
    ,ROW_NUMBER() OVER(partition by City order by count(*) desc) RN
    from 
        (select c.City, g.Name as Genre
        from bus5dwr.dbeaver_sample.Customer c
        inner join bus5dwr.dbeaver_sample.Invoice i
            on i.CustomerId = c.CustomerId
        inner join bus5dwr.dbeaver_sample.InvoiceLine il
            on il.InvoiceId = i.InvoiceId 
        inner join bus5dwr.dbeaver_sample.track t
            on t.TrackId = il.TrackId 
        inner join bus5dwr.dbeaver_sample.Genre g
            on g.GenreId = t.GenreId 
        where Country = 'USA'
        ) as t2
    group by City, Genre)

SELECT City, Genre, Counts 
from q_table
WHERE RN=1
Order BY City

使用 MAX 应该可行。

编辑;添加了内部连接。感谢 Gordon Linoff 的观察,我原来的回答实际上并没有取得任何成就。

with q_table
as 
(   select City, Genre, count(*) as counts
    from 
        (select c.City, g.Name as Genre
        from bus5dwr.dbeaver_sample.Customer c
        inner join bus5dwr.dbeaver_sample.Invoice i
            on i.CustomerId = c.CustomerId
        inner join bus5dwr.dbeaver_sample.InvoiceLine il
            on il.InvoiceId = i.InvoiceId 
        inner join bus5dwr.dbeaver_sample.track t
            on t.TrackId = il.TrackId 
        inner join bus5dwr.dbeaver_sample.Genre g
            on g.GenreId = t.GenreId 
        where Country = 'USA'
        ) as t2
    group by City, Genre)
SELECT a.City, a.Genre, a.counts
FROM q_table a
INNER JOIN (
    SELECT City, MAX(counts) counts
    FROM q_table
    GROUP BY City
) b ON a.City = b.City AND a.counts = b.counts;

试试这个

 with q_table
 as 
 (select * from (
 (   select City, Genre, count(*) as counts
 from 
    (select c.City, g.Name as Genre
    from bus5dwr.dbeaver_sample.Customer c
    inner join bus5dwr.dbeaver_sample.Invoice i
        on i.CustomerId = c.CustomerId
    inner join bus5dwr.dbeaver_sample.InvoiceLine il
        on il.InvoiceId = i.InvoiceId 
    inner join bus5dwr.dbeaver_sample.track t
        on t.TrackId = il.TrackId 
    inner join bus5dwr.dbeaver_sample.Genre g
        on g.GenreId = t.GenreId 
    where Country = 'USA'
    ) as t2
 group by City, Genre)) as t3 where count in (select max(count) count from t3 group by city)