Question

I'm hoping to gain clarity on the behavior of the dense rank function when working with joins. I got a great solution to a post of mine earlier here SQL Server recursive cte help wanted In looking at the solution, which involves using dense rank along with an inner join to partition data with breaks at missing data points.

So: Based on how I read the solution, when in an inner join, dense rank includes in its ranking rows that are on the left side, but not on the right. I set up a small test of this, and I can't reproduce that behavior. For example, when I run the query and I use a basic select statement on the cte, without partitioning it again as it is in the original query, and the data point for 1/5/12, A is missing, I get:

TradeDate     Symbol Clse   DenseRank RowNumber
2012-01-03    A      36.48  1     1
2012-01-04    A      36.19  2     2
2012-01-06    A      37.40  4     3

Based on my current understanding, the dense rank value above would just go from 2 to 3 on the last two records, since the join doesn't return a match for the date in tblDailyPricingAndVol. Thanks in advance.

Was it helpful?

Solution

DENSE_RANK will rank the result of the join. Since you don't have 2012-01-02 in Table2 the result is two rows and DENSE_RANK will give the the ranking values 1 and 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top