【问题标题】:Aggregating table in MySQL excluding character stringMySQL中的聚合表不包括字符串
【发布时间】:2021-03-22 04:26:14
【问题描述】:

所以我有一张如下表:

Metric  Date         Site
GroupA  02/03/2015   Hector.com
GroupA  02/03/2015   Google.com
GroupB  02/03/2015   Hector.com
GroupB  02/03/2015   Booerns.org
GroupC  02/03/2015   abc.com
GroupA  02/04/2015   Jericho.org

我需要把它变成以下格式。我在我的计数中排除了 Site 列中 abc.com 的任何实例。

Metric  Date        Count
GroupA  02/03/2015      2
GroupB  02/03/2015      1
GroupA  02/04/2015      1

所以基本上我想将所有常见的指标组合在一起,并为每一天计算它们。日期出现的次数应等于当天出现的不同指标的不同数量。此外,如果 abc.com 出现在 Site 列中,则字符串中的任何位置(可以像 abc.com/bornhfuuen=email)都不能计算在内

【问题讨论】:

    标签: mysql sql group-by aggregate


    【解决方案1】:

    您可以使用where 删除

    select metric, date, count(*)
    from t
    where site <> 'abc.com'
    group by metric, date;
    

    如果您想删除任何具有'abc.com',您可以使用having

    select metric, date, count(*)
    from t
    group by metric, date
    having sum(site = 'abc.com') = 0;
    

    【讨论】:

      【解决方案2】:

      希望,我正确理解了您的问题,

      请尝试以下查询。

      select metric, date , count(distinct site) as cnt
      from table
      where coalesce(Site,'x') not like '%abc.com%'
      group by metric, date
      

      【讨论】:

        【解决方案3】:

        如果您只需要排除某些行,您可以在对结果进行 GROUP 之前使用 WHERE 子句来做到这一点:

        SELECT Metric, Date, COUNT(1)
        FROM myTable
        WHERE Site NOT LIKE '%abc.com%'
        GROUP BY Metric, Date
        

        (诚然,这是符合 SQL Server 的语法)

        【讨论】:

          【解决方案4】:

          试试这个:

          SELECT Metric, Date, Count(1)
          FROM
            (SELECT Metric, Date
             FROM table1
             WHERE site not like '%abc.com%') T
          GROUP BY Metric, Date
          

          【讨论】:

            猜你喜欢
            • 2020-10-22
            • 2017-07-16
            • 1970-01-01
            • 2016-08-02
            • 1970-01-01
            • 1970-01-01
            • 2020-12-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多