SQLServer SQL query with a row counter

后端 未结 4 1516
花落未央
花落未央 2021-02-01 05:09

I have a SQL query, that returns a set of rows:

SELECT id, name FROM users where group = 2

I need to also include a column that has an incremen

相关标签:
4条回答
  • 2021-02-01 05:54

    The simplest way is to use a variable row counter. However it would be two actual SQL commands. One to set the variable, and then the query as follows:

    SET @n=0;
    SELECT @n:=@n+1, a.* FROM tablename a
    

    Your query can be as complex as you like with joins etc. I usually make this a stored procedure. You can have all kinds of fun with the variable, even use it to calculate against field values. The key is the :=

    0 讨论(0)
  • 2021-02-01 05:55

    In SQL Server 2005 and up, you can use the ROW_NUMBER() function, which has options for the sort order and the groups over which the counts are done (and reset).

    0 讨论(0)
  • 2021-02-01 05:58

    For starters, something along the lines of:

    SELECT my_first_column, my_second_column,
        ROW_NUMBER() OVER (ORDER BY my_order_column) AS Row_Counter
    FROM my_table
    

    However, it's important to note that the ROW_NUMBER() OVER (ORDER BY ...) construct only determines the values of Row_Counter, it doesn't guarantee the ordering of the results.

    Unless the SELECT itself has an explicit ORDER BY clause, the results could be returned in any order, dependent on how SQL Server decides to optimise the query. (See this article for more info.)

    The only way to guarantee that the results will always be returned in Row_Counter order is to apply exactly the same ordering to both the SELECT and the ROW_NUMBER():

    SELECT my_first_column, my_second_column,
        ROW_NUMBER() OVER (ORDER BY my_order_column) AS Row_Counter
    FROM my_table
    ORDER BY my_order_column  -- exact copy of the ordering used for Row_Counter
    

    The above pattern will always return results in the correct order and works well for simple queries, but what about an "arbitrarily complex" query with perhaps dozens of expressions in the ORDER BY clause? In those situations I prefer something like this instead:

    SELECT t.*
    FROM
    (
        SELECT my_first_column, my_second_column,
            ROW_NUMBER() OVER (ORDER BY ...) AS Row_Counter  -- complex ordering
        FROM my_table
    ) AS t
    ORDER BY t.Row_Counter
    

    Using a nested query means that there's no need to duplicate the complicated ORDER BY clause, which means less clutter and easier maintenance. The outer ORDER BY t.Row_Counter also makes the intent of the query much clearer to your fellow developers.

    0 讨论(0)
  • 2021-02-01 06:06

    Heres a different approach. If you have several tables of data that are not joinable, or you for some reason dont want to count all the rows at the same time but you still want them to be part off the same rowcount, you can create a table that does the job for you.

    Example:

    create table #test (
            rowcounter int identity,
            invoicenumber varchar(30)
            )
    
    insert into #test(invoicenumber) select [column] from [Table1]
    
    insert into #test(invoicenumber) select [column] from [Table2]
    
    insert into #test(invoicenumber) select [column] from [Table3]
    
    select * from #test
    
    drop table #test
    
    0 讨论(0)
提交回复
热议问题