TechSapphire Thursday, 2024-04-18, 11:10 AM
Site menu
Login form
News
Play Games
  • Deep Freeze
  • Ice Slide
  • Gyroball
  • Fat Fish
  • Bush Royal Rampage
  • Main » 2012 » September » 3 » Select Alternate Row SQL Server
    9:27 PM
    Select Alternate Row SQL Server
    Select Alternate Row SQL Server

    Their are many approaches to get alternate record from SQL server database table. Here i have discussed 2 approaches
    1. Complex Query
    2. Using Views

    Complex Query: SELECT * from (SELECT ROW_NUMBER() OVER(ORDER BY Employee_id) as RowNumber, * from employees) T where t.RowNumber%2=0

    Using Views:
    CREATE VIEW with_rownumber
    as
    SELECT ROW_NUMBER() OVER(ORDER BY Employee_id) as RowNumber,* from employees;

    SELECT * from with_rownumber where RowNumber%2=0

    The approach preferred here is using VIEW because:
    1. Easy to understand.
    2. In future if you need to perform any operation related to RowNumber then you don't need to create another complex query. You could use view having rownumber column in it.


    Category: Database | Views: 3310 | Added by: Admin | Tags: select, Alternate, ROW NUMBER, Odd, row, number, even, sql, SQL Server | Rating: 0.0/0
    Total comments: 0
    Only registered users can add comments.
    [ Registration | Login ]
    Categories
    Programming [27]
    Tips for programming
    Security [2]
    Security Tips
    Google [1]
    Use google faster then ever you use
    Project [14]
    HTML [2]
    Electronics [0]
    Data Structure [0]
    Database [16]
    SQL SERVER
    SSRS [1]
    Sql Server Reporting Services
    Copyright MyCorp © 2024