【问题标题】:Group or Aggregate functions (min(),max(),sum(),count(),...etc.,) cannot be used within the Group by/Order by/Where/ON clause不能在 Group by/Order by/Where/ON 子句中使用 Group 或 Aggregate 函数(min()、max()、sum()、count()、...等)
【发布时间】:2021-03-26 05:18:27
【问题描述】:

似乎我不能按列的计数排序,这很奇怪,因为我已经看到很多在 ORDER BY 语句中使用 count() 的示例。我正在使用 Zoho Analytics。 这是我的查询:

SELECT "Lead Owner Name", "Lead Source", count("Lead Source")
FROM "Leads"
group by "Lead Owner Name", "Lead Source"
order by count("Lead Source") DESC;

【问题讨论】:

  • 您可以改为按列别名排序吗?

标签: sql zoho


【解决方案1】:

尝试使用别名:

SELECT "Lead Owner Name", "Lead Source", count("Lead Source") as cnt
FROM "Leads" 
GROUP BY "Lead Owner Name", "Lead Source" 
ORDER BY cnt DESC;

大多数数据库都支持ORDER BY 中的任意表达式。但是,有些要求排序表达式实际上是一列——并且他们不检测SELECT 表达式何时与ORDER BY 表达式相同。我之前遇到过这个问题(我认为是 Hive),只是认为这可能适用于您的环境。

【讨论】:

    【解决方案2】:

    您可以在 Zoho Analytics 的 ORDER BY 子句中使用列号。 :)

    SELECT "Lead Owner Name", "Lead Source", count("Lead Source")
    FROM "Leads"
    group by 1,2
    order by 3 DESC
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 2018-03-23
      • 1970-01-01
      • 2020-07-23
      • 2012-05-06
      相关资源
      最近更新 更多