SELECT SearchTerm, CustomerID, CreatedOn
FROM SearchLog
WHERE CreatedOn >= DATEADD(DAY, DATEDIFF(Day, 0, GETDATE()), -5)
GROUP BY SearchTerm, CustomerID, CreatedOn
我想再添加一列,这将是COUNT(SearchTerm),我想通过搜索主机查询主要查询的列。并且列应该说搜索searchterm的次数。 谢谢你的帮助
答案 0 :(得分:0)
如果我理解正确的话,试试你的小组错误 -
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
答案 1 :(得分:0)
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方言)