2011-03-25 147 views
1

我有以下三個查詢可以得到三個特定時間段內產品的平均價格:所有數據,最近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天和所有列表。有我可以做的優化嗎?

回答

1

這樣的事情。確保正確測試

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 
+0

太棒了!我看了這個執行計劃,它比3次選擇要快。 – Martin 2011-03-25 16:52:13

2
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 
1

我沒有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 
1

試試這個:

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