Sql 计算三个时段的平均值

Sql 计算三个时段的平均值,sql,tsql,sql-server-2008,Sql,Tsql,Sql Server 2008,我有以下三个查询,可以得到三个特定时间段内产品的平均价格:所有数据、过去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, Update

我有以下三个查询,可以得到三个特定时间段内产品的平均价格:所有数据、过去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天和所有列表中重复使用。我可以进行优化吗?

我手头没有测试语法的t-SQL,但应该是这样的:

SELECT @Id
       , t1.AggregatedPrice AggregatedPrice_ALL, t1.PCount PCount_ALL
       , t2.AggregatedPrice AggregatedPrice_7, t2.PCount PCount_7
       , t3.AggregatedPrice AggregatedPrice_30, t3.PCount PCount_30
FROM
  (SELECT Id, AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount
     FROM dbo.Products
    WHERE Id = @id) t1
  JOIN 
  (SELECT Id, AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount
     FROM dbo.ApplicationPrice
    WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 7) t2 
  ON t1.Id = t2.id
  JOIN
  (SELECT Id, AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount
     FROM dbo.ApplicationPrice
    WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 30) t3
  ON t1.Id = t3.id
SELECT t1.AggregatedPrice, t1.PCount, t2.AggregatedPrice, t2.PCount, t3.AggregatedPrice, t3.PCount 
FROM
  (SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount  FROM dbo.Products  WHERE Id = @id) t1,
  (SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount  FROM dbo.Products  WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 7) t2,
  (SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount  FROM dbo.Products  WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 30) t3

我手头没有测试语法的t-SQL,但应该是这样的:

SELECT t1.AggregatedPrice, t1.PCount, t2.AggregatedPrice, t2.PCount, t3.AggregatedPrice, t3.PCount 
FROM
  (SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount  FROM dbo.Products  WHERE Id = @id) t1,
  (SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount  FROM dbo.Products  WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 7) t2,
  (SELECT AVG(Price) AS AggregatedPrice, COUNT(*) AS PCount  FROM dbo.Products  WHERE Id = @id AND DATEDIFF(day, UpdatedDatetime, getdate()) < 30) t3
试试这个:

SELECT  AVG(Price) AS AggregatedPrice, 
        COUNT(*) AS PCount,
        AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN Price ELSE NULL END) AS AggregatedWeekPrice, 
        COUNT(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN 1 ELSE NULL END) AS PWeekCount,
        AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN Price ELSE NULL END) AS AggregatedMonthPrice, 
        COUNT(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN 1 ELSE NULL END) AS PMonthCount,
FROM dbo.Products
WHERE Id = @id
试试这个:

SELECT  AVG(Price) AS AggregatedPrice, 
        COUNT(*) AS PCount,
        AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN Price ELSE NULL END) AS AggregatedWeekPrice, 
        COUNT(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN 1 ELSE NULL END) AS PWeekCount,
        AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN Price ELSE NULL END) AS AggregatedMonthPrice, 
        COUNT(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN 1 ELSE NULL END) AS PMonthCount,
FROM dbo.Products
WHERE Id = @id

像这样的。确保测试正确

SELECT
      AVG(Price) AS AggregatedPrice
    , COUNT(*) AS PCount
    , AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN Price ELSE Null End) AS AggregatedPrice7Days
    , SUM(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN 1 ELSE 0 End) AS AggregatedPrice7Days
    , AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN Price ELSE Null End) AS AggregatedPrice30Days
    , SUM(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN 1 ELSE 0 End) AS AggregatedPrice30Days
FROM
    dbo.Products
WHERE
    Id = @id

像这样的。确保测试正确

SELECT
      AVG(Price) AS AggregatedPrice
    , COUNT(*) AS PCount
    , AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN Price ELSE Null End) AS AggregatedPrice7Days
    , SUM(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 7 THEN 1 ELSE 0 End) AS AggregatedPrice7Days
    , AVG(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN Price ELSE Null End) AS AggregatedPrice30Days
    , SUM(CASE WHEN DATEDIFF(day, UpdatedDatetime, getdate()) < 30 THEN 1 ELSE 0 End) AS AggregatedPrice30Days
FROM
    dbo.Products
WHERE
    Id = @id

令人惊叹的我看了这一次的执行计划,它肯定比选择3次要快。太棒了!我看了这一次的执行计划,它肯定比选择3次要快。