title: How to give same rank to rows with same data in some columns then increase when data changed SQL
body: <p>I am trying to use Dense_Rank in my SQL Query to rank rows with same data in some columns, and then increase it when changed data is found in next row. I was successful in doing so by this query: </p>
  
  <pre><code>SELECT DENSE_RANK () OVER (ORDER BY ID, Date1) AS P_Key, 
     *
  FROM dbo.tbl_Main T
  ORDER BY S_ID, ID, Date1, Date2;
  </code></pre>
  
  <p>But the Rank is not coming in the sequential order and is coming according to the order applied on main sql. The output of the script is: </p>
  
  <p><img src="https://i.stack.imgur.com/ehLoG.png" alt="enter image description here"></p>
  
  <p>As visible from the screenshot, it is giving the same rank(P_Key) to the rows which have same data in ID and Date1 columns, but is out of sequence everywhere due to main Order By. I want other rows to be appeared in the same sequence as applied by main "Order By" and the sequence of "P_Key" to be started from 1 and then increasing sequentially. How can I do so?</p>