SQL query to get value of next 5th row without using while loop or cursor in SQL Server 2008 -


i working on query need next nth no of row. table structure

id    stockname stockdate   dayopen dayhigh daylow  dayclose --------------------------------------------------------------------   60    idbi    2014-01-01  66.50   67.80   66.50   67.60  197    idbi    2014-01-02  67.55   69.20   65.25   65.60  334    idbi    2014-01-03  65.00   66.40   64.35   66.15  471    idbi    2014-01-06  66.15   66.35   65.10   65.55  608    idbi    2014-01-07  66.10   66.15   63.85   64.25  745    idbi    2014-01-08  64.00   67.10   63.10   66.80  882    idbi    2014-01-09  66.60   67.80   64.50   64.75 1019    idbi    2014-01-10  65.00   65.90   63.75   64.10 1156    idbi    2014-01-13  63.85   65.00   63.25   64.20 1293    idbi    2014-01-14  64.00   64.95   63.80   64.05 

what want output column name give me next 5th row date

e.g. 1st row new column should return value of next 5th row date ie 2014-01-08 same 2nd row should return 2014-01-09 date.

and can't use datediff -5 day count these working day data excluding weekend days

how value without using while loop or cursor?

use cte return base table plus row_number can left join cte on row_number of new [next5thdate] column being 5 rows ahead of current row:

set nocount on; set ansi_nulls on;  declare @data table (                      id int not null primary key clustered,                      stockname varchar(50) not null,                      stockdate date not null,                      dayopen money not null,                      dayhigh money not null,                      daylow money not null,                      dayclose money not null,                      unique(stockdate)                     )  insert @data values (60, 'idbi', '2014-01-01', 66.50, 67.80, 66.50, 67.60) insert @data values (197, 'idbi', '2014-01-02', 67.55, 69.20, 65.25, 65.60) insert @data values (334, 'idbi', '2014-01-03', 65.00, 66.40, 64.35, 66.15) insert @data values (471, 'idbi', '2014-01-06', 66.15, 66.35, 65.10, 65.55) insert @data values (608, 'idbi', '2014-01-07', 66.10, 66.15, 63.85, 64.25) insert @data values (745, 'idbi', '2014-01-08', 64.00, 67.10, 63.10, 66.80) insert @data values (882, 'idbi', '2014-01-09', 66.60, 67.80, 64.50, 64.75) insert @data values (1019, 'idbi', '2014-01-10', 65.00, 65.90, 63.75, 64.10) insert @data values (1156, 'idbi', '2014-01-13', 63.85, 65.00, 63.25, 64.20) insert @data values (1293, 'idbi', '2014-01-14', 64.00, 64.95, 63.80, 64.05)  ;with cte (     select  d.*, row_number() on (order d.stockdate asc) [rownum]        @data d ) select d1.id, d1.stockname, d1.stockdate, d1.dayopen, d1.dayhigh,        d1.daylow, d1.dayclose, d2.stockdate [next5thdate] cte d1 left join cte d2        on d2.rownum = (d1.rownum + 5) 

results:

id  stockname  stockdate    dayopen dayhigh daylow  dayclose   next5thdate 60    idbi     2014-01-01   66.50   67.80   66.50   67.60      2014-01-08 197   idbi     2014-01-02   67.55   69.20   65.25   65.60      2014-01-09 334   idbi     2014-01-03   65.00   66.40   64.35   66.15      2014-01-10 471   idbi     2014-01-06   66.15   66.35   65.10   65.55      2014-01-13 608   idbi     2014-01-07   66.10   66.15   63.85   64.25      2014-01-14 745   idbi     2014-01-08   64.00   67.10   63.10   66.80      null 882   idbi     2014-01-09   66.60   67.80   64.50   64.75      null 1019  idbi     2014-01-10   65.00   65.90   63.75   64.10      null 1156  idbi     2014-01-13   63.85   65.00   63.25   64.20      null 1293  idbi     2014-01-14   64.00   64.95   63.80   64.05      null 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -