English 中文(简体)
相当于三个时期的平均数
原标题:Compute average for three periods of times

我有以下三点询问:所有数据,最后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天和全部清单。 我是否能够做到最佳?

最佳回答

与此类似。 确保适当测试

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 @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

我没有T-SQL手,来测试yn子,但应该这样:

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




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签