Find the Record - Then Average the "X" Number of Previous Occurrences

时间:2016-02-03 03:33:55

标签: sql-server tsql sql-server-2014

What I hope to accomplish here to to create T-SQL that will find a particular record and display the appropriate projections for that particular record. And also display the average of the three previous times the record occurred.

In the query below, the event was to occur on 10/7/2015 and had two projections of 23 and 23.

SELECT
CONVERT(DATE, DM.FromDateTime) AS 'DriveDate',
Acct.InternalName,
DM.LocationID,
DPaCT.ProcedureProjection,
DPaCT.ProductProjection
FROM
dbo.DriveMaster DM
INNER JOIN dbo.Accounts Acct ON DM.AccountID = Acct.AccountID
INNER JOIN dbo.DriveProjectionAndCollectedTotals DPaCT ON DM.DriveID = DPaCT.DriveID
INNER JOIN dbo.DriveStatusDef Stat ON DM.StatusID = Stat.StatusID
WHERE
Acct.AccountID = 17708
AND DM.FromDateTime = '2015-10-07'

Here are the results - which are as expected and appear correct:

Current Drive

In the second query, I am displaying the three previous times the same event occurred at this location and was marked in a complete status.

SELECT TOP 3
CONVERT(DATE, DM.FromDateTime) AS 'DriveDate',
Acct.InternalName,
DM.LocationID,
DPaCT.ProcedureProjection,
DPaCT.ProductProjection
FROM
Hemasphere.dbo.DriveMaster DM
INNER JOIN dbo.Accounts Acct ON DM.AccountID = Acct.AccountID
INNER JOIN dbo.DriveProjectionAndCollectedTotals DPaCT ON DM.DriveID = DPaCT.DriveID
INNER JOIN dbo.DriveStatusDef Stat ON DM.StatusID = Stat.StatusID
WHERE
Acct.AccountID = 17708
AND DM.FromDateTime < '2015-10-07'
AND DM.StatusID = 2
ORDER BY
DM.FromDateTime DESC;

Past 3 Drives

This is not what I am looking for. Instead of three separate drives, I'd like to have it displayed on in line with the average of the past three drives.

If possible, what I would like to happen have this occur within one statement so that is displays the "upcoming" drive that was to occur on 10/7/2015 and then in calculated columns - average the three previous projections from the event location.

Hopefully this makes sense.

1 个答案:

答案 0 :(得分:0)

如果我正确理解你,那么如何在第一次查询中将第二个查询作为子查询,并在那里平均其结果。像这样:

-- This is your original query
SELECT
CONVERT(DATE, DM.FromDateTime) AS 'DriveDate',
Acct.InternalName,
DM.LocationID,
DPaCT.ProcedureProjection,
DPaCT.ProductProjection,

--This is the sub-query to get the prior projection average
[PriorProcedureProjectionAverage] = (SELECT AVG(ProcedureProjection) FROM (     
  SELECT TOP 3 DPaCT2.ProcedureProjection
  FROM
  Hemasphere.dbo.DriveMaster DM2
  INNER JOIN dbo.Accounts Acct2 ON DM2.AccountID = Acct2.AccountID
  INNER JOIN dbo.DriveProjectionAndCollectedTotals DPaCT2 ON DM2.DriveID = DPaCT2.DriveID
  INNER JOIN dbo.DriveStatusDef Stat22 ON DM2.StatusID = Stat2.StatusID
  WHERE
  Acct2.AccountID = 17708
  AND DM2.FromDateTime < '2015-10-07'
  AND DM2.StatusID = 2
  ORDER BY
  DM2.FromDateTime DESC;
) As SUB)

FROM
dbo.DriveMaster DM
INNER JOIN dbo.Accounts Acct ON DM.AccountID = Acct.AccountID
INNER JOIN dbo.DriveProjectionAndCollectedTotals DPaCT ON DM.DriveID = DPaCT.DriveID
INNER JOIN dbo.DriveStatusDef Stat ON DM.StatusID = Stat.StatusID
WHERE
Acct.AccountID = 17708
AND DM.FromDateTime = '2015-10-07'

这有点乱,如果你也想要,你必须为ProductProjection添加第二个子查询。但希望这会给你一个想法。