我有以下三点询问:所有数据,最后7天,最后30天。
SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount
FROM dbo.Products
WHERE Id = @id
SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount
FROM dbo.Products
WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 7
SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount
FROM dbo.Products
WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 30
这三个问题给我提供了正确的数据,但并非我所希望的形式。 是否有办法将这三个问题合并成一个问题。 我的最终目标是,把所有数据放在一行。
此外,在我看来,7天的平均计算结果可以重新使用30天和全部清单。 我是否能够做到最佳?