Home Forums SQL Server 2008 SQL Server Newbies Counting how many times a value appears in a column and assigning an incremental number to each one. RE: Counting how many times a value appears in a column and assigning an incremental number to each one.<!-- 864 -->

  • Ed Wagner (8/20/2014)


    Lynn Pettis (8/20/2014)


    Lynn Pettis (8/20/2014)


    Ed Wagner (8/20/2014)


    This is one of the great things about this site. A question gets posted and several working solutions get posted. Then we can all learn something from the different ways.

    Lynn, I learned about the COUNT(*) OVER from your post. I tried it and it worked, so I looked it up on MSDN at http://msdn.microsoft.com/en-us/library/ms175997%28v=sql.100%29.aspx. The 2008 and 2012 pages don't have the OVER clause at all, but the 2014 does. It does work in SQL 2008 SP3. Thanks for posting it because I learned something new, which is always a great way to start the day.

    Actually, it also works in SQL Server 2005. That's where I first learned about using COUNT(*) OVER.

    Check out Example B (and this is for SQL Server 2005):

    http://msdn.microsoft.com/en-us/library/ms189461(v=sql.90).aspx

    And yet the COUNT function: http://msdn.microsoft.com/en-us/library/ms175997%28v=sql.100%29.aspx.

    It includes the OVER clause for 2014, but not for the others. I guess it's just where you look. Thanks, Lynn. I always appreciate learning something that I'll likely use frequently. 😉

    Actually, the OVER clause has the same example for all versions since 2005. The aggregate functions' articles were changed for 2014 but lack the OVER clause in previous versions.

    I'm sure I got there from the ranking functions article.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2