【问题标题】:using SQL aggregate functions with JOINs使用带有 JOIN 的 SQL 聚合函数
【发布时间】:2011-05-26 04:21:49
【问题描述】:

我有两个表 - tool_downloads 和 tool_configurations。我正在尝试检索数据库中每个工具的最新构建日期。 DB的布局很简单。一个名为 tool_downloads 的表会跟踪下载工具的时间。另一个表称为 tool_configurations 并存储有关该工具的实际数据。它们通过 tool_conf_id 链接在一起。

如果我运行以下省略日期的查询,我会返回 200 条记录。

SELECT DISTINCT a.tool_conf_id, b.tool_conf_id
FROM tool_downloads a
JOIN tool_configurations b 
ON a.tool_conf_id = b.tool_conf_id
ORDER BY a.tool_conf_id

当我尝试添加日期信息时,我得到了数十万条记录!这是严重失败的查询。

SELECT DISTINCT a.tool_conf_id, max(a.configured_date) as config_date, b.configuration_name
FROM tool_downloads a
JOIN tool_configurations b
ON a.tool_conf_id = b.tool_conf_id
ORDER BY a.tool_conf_id

我知道问题与分组/聚合数据和连接有关。我无法真正搜索谷歌,因为我不知道我遇到的问题的名称。任何帮助,将不胜感激。

【问题讨论】:

    标签: join group-by sybase aggregate


    【解决方案1】:

    解决办法是:

    SELECT b.tool_conf_id, b.configuration_name, max(a.configured_date) as config_date
      FROM tool_downloads a
        JOIN tool_configurations b
          ON a.tool_conf_id = b.tool_conf_id
      GROUP BY b.tool_conf_id, b.configuration_name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 2016-08-15
      相关资源
      最近更新 更多