【问题标题】:How to get two ties of maximum values in my query?如何在我的查询中获得两个最大值的关系?
【发布时间】:2019-10-28 15:51:26
【问题描述】:

我有一个音乐商店的数据库,我需要从每个国家/地区的特定客户那里提取最大购买价值。在使用MAX 函数时,我注意到我在“英国”中有两个最大值的关系。所以,我需要我的查询来返回这个国家的两个客户。

With t1 As (
           Select i.CustomerId, c.FirstName, c.LastName, 
                      i.BillingCountry Country, Sum(i.Total) Totals
             From Invoice i
              Join Customer c
              On c.CustomerId = i.CustomerId
             GROUP BY 1, 4)

Select CustomerId, FirstName, LastName, Country, Max(Totals) TotalSpent
    From t1
    Group By 4;

这是输出

这就是输出应该是什么

我尝试使用TOP,但显然工作区不接受此功能。所以,请提出一个不使用此功能的解决方案。

提前致谢。

【问题讨论】:

    标签: sql max aggregation


    【解决方案1】:

    我会考虑类似的东西

    With t1 As (
               Select i.CustomerId, c.FirstName, c.LastName, 
                          i.BillingCountry Country, Sum(i.Total) Totals
                 From Invoice i
                  Join Customer c
                  On c.CustomerId = i.CustomerId
                 GROUP BY 1, 4)
    
    Select CustomerId, FirstName, LastName, Country, Totals TotalSpent
        From t1
        WHERE t1.Total = (SELECT MAX(Totals) FROM t1 t2 WHERE t1.Country = t2.Country)
        Group By 4;
    

    (我在主查询的SELECT 语句中将MAX(Totals) 更改为Totals 并添加了WHERE 子句)

    With t1 As (
               Select i.CustomerId, c.FirstName, c.LastName, 
                          i.BillingCountry Country, Sum(i.Total) Totals
                 From Invoice i
                  Join Customer c
                  On c.CustomerId = i.CustomerId
                 GROUP BY 1, 4),
    t2 as (
     SELECT Country, MAX(Totals) as Totals
     FROM t1
     GROUP BY Country
    )
    Select t1.CustomerId, t1.FirstName, t1.LastName, t1.Country, t1.Totals TotalSpent
        From t1 INNER JOIN t2
         on t1.Country = t2.Country and t1.Totals = t2.Totals
        Group By 4;
    

    (我已经添加了 t2 CTE,将其加入到您的主查询中,并相应地调整了主 SELECT

    在这两种情况下,我都试图选择所有客户信息,其中该客户的总数等于他们所在国家/地区的最大总数。原则上,无论有多少关系,这都应该有效。

    【讨论】:

    • 我认为 group by 会抛出错误,因为并非 group by 子句中的所有列都在 select 语句中。我认为您需要修改查询以具有 GROUP BY i.CustomerId、c.FirstName、c.LastName、i.BillingCountry。
    • 我离开了 OP 的 group bys,因为他们没有提到他们的问题。你是对的,他们会在大多数 DBMS 中抛出错误(所以我假设 OP 在合法的地方使用 MySql),但这似乎与这里的问题是分开的。
    • 不幸的是,我在这两个查询中都得到了与以前相同的结果。我理解第二个,但我看到 t2 只包含第一个最大值,所以在加入 t1 时它不会添加任何值。
    • 你用的是什么关系型数据库?
    • 对不起,我想不通这个问题。这是一个在线评估,我正在开发一个在线工作区。 @VenkataramanR
    【解决方案2】:

    使用窗口函数!

    select CustomerId, FirstName, LastName, 
           Country, Totals
    from (select i.CustomerId, c.FirstName, c.LastName, 
                 i.BillingCountry as Country, sum(i.Total) as Totals,
                 rank() over (partition by i.BillingCountry over sum(i.Total) desc) as seqnum
          from Invoice i join
               Customer c
               on c.CustomerId = i.CustomerId
          group by i.CustomerId, c.FirstName, c.LastName, 
                   i.BillingCountry
         ) ic
    where seqnum = 1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 2020-09-22
      • 2021-07-06
      • 1970-01-01
      • 2020-03-29
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多