【问题标题】:sql query for the following statement?sql查询下面的语句?
【发布时间】:2016-03-11 07:10:47
【问题描述】:

我的表“客户”包含客户 ID、名字、姓氏、公司、城市、州、国家、电子邮件、发票总额

问题:对于至少有两个客户使用雅虎作为电子邮件提供商的国家/地区,请在收入旁边显示名称

我的解决方案:

select county,sum(invoiceTotal)from customer where email like '%yahoo%'
group by Country,Email having Count(Country)>2

我无法得到正确的结果,我的输出中显示的行数与预期输出中的行数不同,谁能告诉我哪里出错了???

【问题讨论】:

  • 您应该只按国家/地区分组,如果您按电子邮件分组,则每封电子邮件都会得到一个组

标签: sql


【解决方案1】:

您也按email 分组 - 只需按Country 分组就可以了

select 
    county,
    sum(invoiceTotal)
from customer 
where email like '%yahoo%'
group by Country
having Count(Country)>2

【讨论】:

  • 你的代码略有变化,至少有 Count(Country)>=2,感谢你的帮助
【解决方案2】:

您不能按email 分组,因为这是独一无二的。幸运的是,您不必这样做。

【讨论】:

    【解决方案3】:
    select 
        county,
        sum(invoiceTotal)
    from customer 
    where email like '%yahoo%'
    group by Country
    having Count(Country)>=2
    

    因为声明说至少那么你需要输入>=。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多