我正在尝试在表格中显示计数和百分比。
我使用的查询是这样:
DECLARE @BeginDate AS DATETIME
SET @BeginDate = GETDATE();
SELECT TOP 10
s.Title AS Title, COUNT(*) AS TotalSessions
FROM
History s
WHERE
CONVERT(DATE, s.DateStamp) >= DATEADD(DAY, -7, @BeginDate)
AND CONVERT(DATE, s.DateStamp) <= DATEADD(DAY, -1, @BeginDate)
GROUP BY
Title
ORDER BY
TotalSessions DESC
这将返回前1o条记录,现在返回
我想在第三栏中显示相对于总数的百分比值。我可以在同一查询中执行此操作吗?
我想显示其余计数(如果有100条记录,则前10行显示前10条记录,第11行显示标题为“ Others”的其余90条记录的总和)。我可以在同一查询中这样做吗?
答案 0 :(得分:1)
您可以使用窗口功能。像这样:
SELECT TOP 10 s.Title as Title, count(*) as TotalSessions,
COUNT(*) * 1.0 / SUM(COUNT(*)) OVER ()
FROM History s
WHERE convert(date,s.DateStamp) >= DATEADD(DAY, -7, @BeginDate)
AND convert(date,s.DateStamp) <= DATEADD(DAY, -1, @BeginDate)
GROUP BY Title
ORDER BY TotalSessions DESC