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


what hope accomplish here to create t-sql find particular record , display appropriate projections particular record. , display average of 3 previous times record occurred.

in query below, event occur on 10/7/2015 , had 2 projections of 23 , 23.

select convert(date, dm.fromdatetime) 'drivedate', acct.internalname, dm.locationid, dpact.procedureprojection, dpact.productprojection 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 acct.accountid = 17708 , dm.fromdatetime = '2015-10-07' 

here results - expected , appear correct:

current drive

in second query, displaying 3 previous times same event occurred @ location , marked in complete status.

select top 3 convert(date, dm.fromdatetime) 'drivedate', acct.internalname, dm.locationid, dpact.procedureprojection, dpact.productprojection 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 acct.accountid = 17708 , dm.fromdatetime < '2015-10-07' , dm.statusid = 2 order dm.fromdatetime desc; 

past 3 drives

this not looking for. instead of 3 separate drives, i'd have displayed on in line average of past 3 drives.

if possible, happen have occur within 1 statement displays "upcoming" drive occur on 10/7/2015 , in calculated columns - average 3 previous projections event location.

hopefully makes sense.

if i'm understanding correctly, how making second query subquery in first, , averaging results there. this:

-- original query select convert(date, dm.fromdatetime) 'drivedate', acct.internalname, dm.locationid, dpact.procedureprojection, dpact.productprojection,  --this sub-query prior projection average [priorprocedureprojectionaverage] = (select avg(procedureprojection) (        select top 3 dpact2.procedureprojection     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     acct2.accountid = 17708   , dm2.fromdatetime < '2015-10-07'   , dm2.statusid = 2   order   dm2.fromdatetime desc; ) sub)  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 acct.accountid = 17708 , dm.fromdatetime = '2015-10-07' 

that's little messy, , you'd have add second subquery productprojection if want too. gives idea.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -