我想创建一个报告,显示sql server表中数据的统计信息。我感兴趣的表列是searchTerm,dateadded。 现在我想获得每个searchterm的记录数,但也显示该searchterm的最早和最新dateadded。 例如:
select searchterm, count(*) as recCount from SearchTable order by searchterm.
只会给我计数,所以我只需要日期和不同的记录。
答案 0 :(得分:3)
试试这个:
SELECT
searchterm,
MIN(dateadded) AS minDateAdded,
MAX(dateadded) AS maxDateAdded,
count(*) as recCount
FROM SearchTable
GROUP BY searchterm
他们GROUP BY将确保每一行都是不同的。
答案 1 :(得分:1)
您可以使用MIN和MAX函数以及GROUP BY子句
SELECT searchterm, MIN(dateadded) AS earliest, MAX(dateadded) AS latest, COUNT(*) as recCount
FROM SearchTable
GROUP BY searchterm
ORDER BY searchterm