Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the info of the given date / closet date to the given date
    primarykey
    data
    text
    <p>I am trying to find whether the <strong><code>status</code></strong> of an <strong><code>ID</code></strong> is <strong><code>Active/Backup</code></strong> on a given date/the closest date to the given date.</p> <p>Data in my CTE :</p> <pre><code>ID StatusDate Status Order 2145 2012-04-29 n/a 1 2145 2012-08-02 Backup 2 2145 2012-09-27 Backup 3 2145 2012-11-07 Backup 4 2145 2012-11-09 Active 5 2145 2012-11-12 Backup 6 2145 2012-12-13 Pending 7 2145 2012-12-18 Sold 8 2146 2012-10-15 Pending 1 2146 2012-10-15 n/a 2 2146 2012-12-19 Sold 3 4145 2012-04-24 Active 1 4145 2012-04-24 Active 2 4145 2012-05-22 Pending 3 4145 2012-09-13 Active 4 4145 2012-09-13 Active 5 4145 2012-12-05 Pending 6 4145 2012-12-19 Sold 7 7175 2012-11-08 n/a 1 7175 2012-12-01 Backup 2 7175 2012-12-05 Active 3 7175 2012-12-06 Pending 4 7175 2012-12-19 Sold 5 </code></pre> <p>Result :</p> <pre><code>Analysis 09/20/2012 12/19/2012 3/20/2013 Total Active 2 0 0 </code></pre> <p>The headers for the result should be : the date 6 months ago from current date, 3 months ago from current date and current date.**</p> <p>Here is the query I am struggling with : </p> <pre><code> ;WITH x AS ( SELECT ID,statusdate,status , row_number() over (partition by ID order by statusdate DESC ) as RN1 FROM (SELECT ID,statusdate,status, rn = row_number() over (partition by ID order by statusdate ) FROM tblHistory (nolock) WHERE [statusdate] &lt;= '20120920' AND ID in ('2145','2146','4145''7175') ) AS A ) SELECT ID,statusdate,status FROM x WHERE rn1 = 1 AND status IN ('Backup','Active') </code></pre>
    singulars
    1. This table or related slice is empty.
    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