Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's another approach:</p> <p><strong>Step 1.</strong> Select rows with <code>Date</code> values no later than the selected month, splitting every job's data into two partitions: the selected month data goes into the <code>New</code> partition, the rest goes into the <code>Old</code> one.</p> <p>Applied to your example, this would produce the following result set:</p> <pre> Job Date Percent_complete <b>recency</b> --- ---- ---------------- <b>-------</b> 1 1/5/2013 5 <b>Old </b> 2 1/10/2013 5 <b>Old </b> 2 1/25/2013 15 <b>Old </b> 2 2/15/2013 25 <b>New </b> 3 1/15/2013 5 <b>New </b> 1 2/10/2013 10 <b>New </b> 1 2/23/2013 20 <b>New </b> 1 2/24/2013 18 <b>New </b> 4 1/12/2013 40 <b>Old </b> </pre> <p><strong>Step 2.</strong> Rank rows in every partition in the descending order of <code>Date</code>:</p> <pre> Job Date Percent_complete recency <b>rnk</b> --- ---- ---------------- ------- <b>---</b> 1 1/5/2013 5 Old <b>1 </b> 2 1/10/2013 5 Old <b>2 </b> 2 1/25/2013 15 Old <b>1 </b> 2 2/15/2013 25 New <b>1 </b> 3 1/15/2013 5 New <b>1 </b> 1 2/10/2013 10 New <b>3 </b> 1 2/23/2013 20 New <b>2 </b> 1 2/24/2013 18 New <b>1 </b> 4 1/12/2013 40 Old <b>1 </b> </pre> <p><strong>Step 3.</strong> Get only rows with the rankings of 1:</p> <pre> Job Date Percent_complete recency rnk --- ---- ---------------- ------- --- 1 1/5/2013 5 Old 1 2 1/25/2013 15 Old 1 2 2/15/2013 25 New 1 3 1/15/2013 5 New 1 1 2/24/2013 18 New 1 </pre> <p><strong>Step 4.</strong> Pivot the percentages and default any <code>Old</code> NULLs to 0, and <code>New</code> to <code>Old</code>:</p> <pre> Job Old New --- --- --- 1 5 18 2 15 25 3 0 5 </pre> <p>The following query implements the above logic:</p> <pre><code>WITH partitioned AS ( SELECT Job, Date, Percent_complete, recency = CASE WHEN Date &gt;= '20130201' THEN 'NEW' ELSE 'Old' END FROM atable WHERE Date &lt; '20130301' ) , ranked AS ( SELECT Job, Date, Percent_complete, recency, rnk = ROW_NUMBER() OVER (PARTITION BY Job, recency ORDER BY Date DESC) FROM partitioned ) , filtered AS ( SELECT Job, Percent_complete, recency FROM ranked WHERE rnk = 1 ) , pivoted AS ( SELECT Job, Old = ISNULL(Old, 0), New = ISNULL(New, Old) FROM filtered PIVOT ( MAX(Percent_complete) FOR recency IN (Old, New) ) u ) SELECT * FROM pivoted ; </code></pre> <p>You can test this query <a href="http://sqlfiddle.com/#!3/c1d05/1" rel="nofollow">at SQL Fiddle</a>.</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.
 

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