我真的认为你应该规范化你的数据库模式,它会让这样的查询很容易解决。
例如 - 我认为 Stadium 和 Game 应该是 2 个单独的表,具有一对多的链接。(如果一场比赛可以跨越不同的体育场,甚至是多对多)。
但是对于您的示例:
SELECT s.Number AS Stadium, s.[Date], COUNT(s.[Date]) AS 'Count'
FROM Stadium s
GROUP BY s.Number, s.[Date]
ORDER BY s.Number, COUNT(s.[Date]) DESC
假设 Stadium 表上有一个 Id,这将适用于您的第二个查询 - 但不确定是否有更有效的方法。
SELECT s.Number AS Stadium,
COUNT(y1.[Date]) AS Year1Count,
COUNT(y2.[Date]) AS Year2Count,
COUNT(y1.[Date]) / COUNT(y2.[Date]) * 100 AS Growth
FROM Stadium s
LEFT OUTER JOIN Stadium y1 ON s.Id = y1.Id AND y1.[Date] BETWEEN '2011-01-01 00:00:00' AND '2011-12-31 23:59:59'
LEFT OUTER JOIN Stadium y2 ON s.Id = y2.Id AND y2.[Date] BETWEEN '2012-01-01 00:00:00' AND '2012-12-31 23:59:59'
GROUP BY s.Number
HAVING COUNT(y1.[Date]) / COUNT(y2.[Date]) * 100 > 25