I've been working on a semi-awkward query in that it uses a very high number of functions given its relatively small size and scope. I was hoping to get some feedback on any ways I could format or re-factor this better?
select Name ,
avg(TimeProcessing / 1000 + TimeRendering / 1000 + TimeDataRetrieval / 1000) as 'Current Month' ,
isnull(count(TimeProcessing), 0) as 'Sample' ,
min(l2.[Avg Exec Previous Month]) as 'Previous Month' ,
isnull(min(l2.[Sample Previous Month]), 0) as 'Sample' ,
min(l3.[Avg Exec Two Months Ago]) as 'Two Months ago' ,
isnull(min(l3.[Sample Two Months Ago]), 0) as 'Sample'
from marlin.report_execution_log l
inner join marlin.report_catalog c on l.ReportID = c.ItemID
left outer join ( select l2.ReportID ,
avg(TimeProcessing / 1000 + TimeRendering
/ 1000 + TimeDataRetrieval / 1000) as 'Avg Exec Previous Month' ,
count(TimeProcessing) as 'Sample Previous Month'
from marlin.report_execution_log l2
where TimeEnd between dateadd(MONTH, -2,
getdate())
and dateadd(MONTH, -1,
getdate())
group by l2.ReportID
) l2 on l.ReportID = l2.ReportID
left outer join ( select l3.ReportID ,
avg(TimeProcessing / 1000 + TimeRendering
/ 1000 + TimeDataRetrieval / 1000) as 'Avg Exec Two Months Ago' ,
count(TimeProcessing) as 'Sample Two Months Ago'
from marlin.report_execution_log l3
where TimeEnd between dateadd(MONTH, -3,
getdate())
and dateadd(MONTH, -2,
getdate())
group by l3.ReportID
) l3 on l.ReportID = l3.ReportID
group by l.ReportID, Name
order by Name
1
for 'January') by using theAVG()
function, you should probably renameCurrent Month
to whatever the actual measure is, including units - perhapsAverage Time Elapsed in Seconds
or something. And you might consider changing the operations to(TimeProcessing + TimeRendering + TimeDataRetrieval) / 1000
, which will guarantee that the same conversion is used for each field. \$\endgroup\$SELECT AVG()
is only getting the current month? Without aWHERE
clause, it won't restrict it - not if the previous two months are also in the table. Also, be careful when using between - currently, anything equal todateadd(MONTH, -2, getdate())
will be counted twice - once forl2
, and once forl3
- I reccomend exclusive upper bounds. \$\endgroup\$