Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Special counter query in SQL server 2005

Posted on 2013-10-23
5
Medium Priority
?
304 Views
Last Modified: 2013-10-23
I have to write a stored procedure which will get as parameter two numbers
The first is a value to "locate" in a table T1, the second is a kind of "step"
Here is a sample of my table (ordered ascending on C1)

T1
4
10
18
34
69
76
101
.../...

When the procedure receives, says value 37, and second parameter is 3, it must return the 3rd (3) value of the table, greater than 37
Is it possible in a single query or do I need a cursor and count lines ?
Single query is prefered solution
Thanks
0
Comment
Question by:LeTay
  • 3
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39593543
This returns c1=101 with inputs of 37 and 3:
DECLARE @Val int, @Step int

SET @Val = 37
SET @Step = 3

SELECT
       c1
FROM (
       SELECT c1, row_number() over (PARTITION BY CASE WHEN c1 > @Val THEN 0 ELSE 1 END
                                     ORDER BY c1) AS rn
       FROM t1
     ) AS derived
WHERE c1 > @Val
AND rn = @Step
;

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39593560
this might be better:
DECLARE @Val int, @Step int

SET @Val = 37
SET @Step = 3

SELECT TOP 1
       c1
FROM (
       SELECT top (select @step)
         c1
       FROM t1
       WHERE c1 > @Val
       ORDER BY C1
     ) AS derived
ORDER BY
      C1 DESC
;

Open in new window

0
 

Author Closing Comment

by:LeTay
ID: 39593928
Wonderful result, but I do not understand it at all ...
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39593963
did you try the second one? I think it will be faster.

row_number() just assign unique integer values starting at 1 and increasing by 1

the "partition by" is a bit like a "group by" this is a range over which the numbers will be assigned

order by determines where 1, 2, 3... is assigned

so the partition is all numbers > @val; or all numbers <= @val (i.e. 2 partitions)
order by says start at 1 for the number immediately after @Val

that number can be matched to @step

the second one is easier

select just the top number = to @step
where the C1 values > @val
ordered by C1

from that result reverse the order, so, now the last record becomes the first record
and just list that record by 'top 1'
0
 

Author Comment

by:LeTay
ID: 39593978
Sorry I missed it
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question