代码之家  ›  专栏  ›  技术社区  ›  Laziale

SQL查询,该查询按

sql
  •  1
  • Laziale  · 技术社区  · 14 年前
    SELECT SearchTerm, CustomerID, CreatedOn
    FROM         SearchLog 
    WHERE     CreatedOn >= DATEADD(DAY, DATEDIFF(Day, 0, GETDATE()), -5)
    GROUP BY SearchTerm, CustomerID, CreatedOn
    

    我想再添加一列,它是count(searchterm),我想按搜索词排序,搜索词主要是搜索的。该列应该说明搜索词的搜索次数。 谢谢你的帮助

    2 回复  |  直到 14 年前
        1
  •  0
  •   Vishal    14 年前

    如果我理解正确的话,你的小组就错了-

    SELECT d1.CustomerId,SearchTerm,CreatedOn,d1.TimesSearched
    FROM
     (
        SELECT CustomerID, COUNT(SearchTerm) as TimesSearched
            FROM SearchLog 
            WHERE CreatedOn >= DATEADD(DAY, DATEDIFF(Day, 0, GETDATE()), -5)
            GROUP BY CustomerID
            ORDER BY COUNT(SearchTerm) desc
    ) as d1 inner join SearchLog on
       d1.CustomerID=SearchLog .CustomerId
    
        2
  •  0
  •   Uhlen    14 年前

    CreatedOn 有没有时间戳? 如果只是日期部分,你可以做:

    SELECT   s.SearchTerm, s.CustomerID, s.CreatedOn, COUNT(1) AS search_count
    FROM     SearchLog s
    WHERE    CreatedOn >= DATEADD(DAY, DATEDIFF(Day, 0, GETDATE()), -5)
    GROUP BY  s.SearchTerm, s.CustomerID, s.CreatedOn
    ORDER BY COUNT(1)
    

    否则,您需要以某种方式除去时间戳(我不知道您的SQL方言)