Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I started to think about it and wondered if there was a way to streamline it and do so without temp tables. Since everything I've been doing lately seems to involved CTEs I worked up a solution going that route. This was the result, it might not be the most efficient way to go about it from a CTE perspective but I prefer it to a bunch of temp tables and it leaves most of the nasty casting out of the final <code>SELECT</code>:</p> <pre><code> ;WITH CompanyDays AS ( SELECT Company ,MIN(ActualStartTime) AS StartDate ,MAX(endtime) AS EndDate FROM Companies WHERE finaljobstatus = '19' OR (finaljobstatus = '3' AND tasktypeid = '100') GROUP BY Company ) ,StartFileSize AS ( SELECT c.Company ,c.totaldatasizebytes AS StartFileSize ,cd.StartDate ,cd.EndDate FROM Companies AS c JOIN CompanyDays AS cd ON c.actualstarttime= cd.StartDate WHERE (finaljobstatus = '19' OR (finaljobstatus = '3' AND tasktypeid = '100')) ) ,EndFileSize AS ( SELECT c.Company ,CAST(s.StartFileSize AS DECIMAL(18,0)) AS StartFileSize ,CAST(c.totaldatasizebytes AS DECIMAL(18,0)) AS EndFileSize ,DATEDIFF(dd,cd.StartDate,cd.EndDate) AS DayDiff ,CAST(c.totaldatasizebytes AS DECIMAL(18,0)) - CAST(s.StartFileSize AS DECIMAL(18,0)) AS FileDiff ,cd.StartDate ,cd.EndDate FROM Companies AS c JOIN CompanyDays AS cd ON c.endtime= cd.EndDate JOIN StartFileSize AS s ON c.Company = s.Company WHERE (finaljobstatus = '19' OR (finaljobstatus = '3' AND tasktypeid = '100')) ) SELECT e.Company ,DayDiff ,FileDiff ,(FileDiff/e.StartFileSize)/DayDiff AS AveragePercentageChange FROM EndFileSize AS e </code></pre> <p>Note that <code>DATEDIFF</code> on the actual start and end dates with times might skew things slightly because it only counts the actual day boundaries that are crossed (i.e., Midnight). In most cases, that will be perfectly fine. If not, you can take another measure like datediff in hours and divide the result by 24; minutes and divide it by 1440; seconds and divide it by 86400 depending on exactly how precise you need the day difference to be.</p> <p>EDIT: I realized my math was off. What if there was a decrease between the two days? Fixed that and moved some more casting into the CTE.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload