【问题标题】:How to do I query all distinct rows with only their highest values?如何查询所有仅具有最高值的不同行?
【发布时间】:2020-12-02 01:12:12
【问题描述】:

我一直在尝试查询每个城市的流行类型。我只是想获取我突出显示的行。我尝试在 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)

我尝试了以下查询。

【问题讨论】:

  • 请不要使用格式化文本可以使用的图像。
  • @DaleK 抱歉。我还是新手。我认为这将为其他用户提供最好的视角来研究问题。

标签: sql sql-server tsql group-by distinct


【解决方案1】:

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

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

【讨论】:

  • 非常感谢@JMabee。使用OVER()ROW_NUMBER() 组合解决了它。
  • 如果波士顿的拉丁语和金属计数均为 12 怎么办?只会包含一个,对吗?
  • @John Mitchell:是的。如果两者都需要,请将ROW_NUMBER 替换为RANKDENSE_RANK
【解决方案2】:

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;

【讨论】:

  • 感谢指出,我已相应编辑。
【解决方案3】:

试试这个

 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)

【讨论】:

  • 首先,您的“计数”应该是“计数”。其次,这不会返回不同的城市、流派组合。如果在示例中,Cupertino 的计数值也为 12;那么这也会显示,因为 12 是不同城市的最大计数。
  • @chris 我同意约翰的观点。但是感谢您的帮助。
猜你喜欢
  • 2021-01-23
  • 1970-01-01
  • 2014-11-03
  • 2014-11-17
  • 2023-01-30
  • 2019-09-08
  • 2015-07-30
相关资源
最近更新 更多