Showing posts with label fetch. Show all posts
Showing posts with label fetch. Show all posts

Wednesday, March 21, 2012

Row offset values

What is the fastest way to select a value offset by n rows from the start row? I used to use a cursor with FETCH ABSOLUTE in Sybase SQLAnywhere, but this is incredibly slow in SQL Server. Here's the current function I'm using:

FUNCTION dbo.TradingDaysBack ( @.ItemID int, @.FromDate smalldatetime, @.DaysBack int )
RETURNS smalldatetime
AS
BEGIN
declare @.BackDay int
declare @.OADay int
set @.OADay = dbo.GetOADate(@.FromDate)
declare curDaysBack cursor scroll for
select OADate
from Data_Daily
where ItemID = @.ItemID and OADate <= @.OADay
order by OADate desc

open curDaysBack
fetch absolute @.DaysBack
from curDaysBack
into @.BackDay

close curDaysBack
deallocate curDaysBack

if @.BackDay is null
begin
set @.BackDay= ( select Min(OADate) from Data_Daily where ItemID = @.ItemID and OADate <= @.OADay )
end

RETURN convert(smalldatetime, @.BackDay)

END

The idea is to get the date n rows of data back from the starting date (i.e. 30 trading days back from 12/1/2003). Any ideas?DATEDIFF?

You know your example is only selecting 1 row....|||It can't be DateDiff, because not every day is a trading day, obviously. I need to go back n trading days, meaning entries for the given ticker between two dates. And yes, it is only selecting one row, which is the idea.sql

Monday, March 12, 2012

ROW ID

Can any one guid me like I have a billions of records and I want to see only
10th record and I want to fetch the 10th record through rowid not the Unique
primary key.
Let's say Table primary key id start from
6380,
7066,
7067,
7131,
7896,
8042
and so on ...
If I wanna fetch third record which ID = 7067 so I have to give 7067 but I
want to give input parameter = 3 then it will show me the third record, I am
talking about row id... is there any thing in SQL Server or any alternate
way through which I can solve my problem.
Thanks
Hi,
There is no row id concept in SQL Server.
Thanks
Hari
SQL Server Mvp
"Roy" <roy@.hotmail.com> wrote in message
news:OKMKgoqdFHA.1384@.TK2MSFTNGP09.phx.gbl...
> Can any one guid me like I have a billions of records and I want to see
> only
> 10th record and I want to fetch the 10th record through rowid not the
> Unique
> primary key.
> Let's say Table primary key id start from
> 6380,
> 7066,
> 7067,
> 7131,
> 7896,
> 8042
> and so on ...
> If I wanna fetch third record which ID = 7067 so I have to give 7067 but I
> want to give input parameter = 3 then it will show me the third record, I
> am
> talking about row id... is there any thing in SQL Server or any alternate
> way through which I can solve my problem.
> Thanks
>
>
|||Just to add there will be one in SQL2005
Andrew J. Kelly SQL MVP
"Hari Prasad" <hari_prasad_k@.hotmail.com> wrote in message
news:efaBvgudFHA.640@.tk2msftngp13.phx.gbl...
> Hi,
> There is no row id concept in SQL Server.
> Thanks
> Hari
> SQL Server Mvp
> "Roy" <roy@.hotmail.com> wrote in message
> news:OKMKgoqdFHA.1384@.TK2MSFTNGP09.phx.gbl...
>