Probably a stupid query question...

  • I need a column in my query results that just numbers the rows sequentially (i.e. 1, 2, 3). How can I do that? Thanks.

  • With ROW_NUMBER. https://msdn.microsoft.com/en-us/library/ms186734.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thank you

  • With regards to your thread's subject:

    The only stupid query questions are the ones you don't ask.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (4/20/2015)


    With regards to your thread's subject:

    The only stupid query questions are the ones you don't ask.

    Lol I feel like it was an easy question but I'm also a programmer having to pretend to also be a dba and a network engineer.

  • Sean Grebey (4/20/2015)


    dwain.c (4/20/2015)


    With regards to your thread's subject:

    The only stupid query questions are the ones you don't ask.

    Lol I feel like it was an easy question but I'm also a programmer having to pretend to also be a dba and a network engineer.

    -- We speak in SQL-tongue here

    DECLARE @LowHangingFruit BIT;

    SELECT @LowHangingFruit=AnyQuestion

    FROM AnyTopic

    WHERE simple <> dumb;


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (4/20/2015)


    Sean Grebey (4/20/2015)


    dwain.c (4/20/2015)


    With regards to your thread's subject:

    The only stupid query questions are the ones you don't ask.

    Lol I feel like it was an easy question but I'm also a programmer having to pretend to also be a dba and a network engineer.

    -- We speak in SQL-tongue here

    DECLARE @LowHangingFruit BIT;

    SELECT @LowHangingFruit=AnyQuestion

    FROM AnyTopic

    WHERE simple <> dumb;

    -- We speak in SQL-tongue here

    DECLARE @LowHangingFruit BIT;

    SELECT @LowHangingFruit=AnyQuestion

    FROM AnyTopic

    WHERE 'simple' <> 'dumb';

    :-P:-P:-P

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply