Comments on: How to simulate the SQL ROW_NUMBER function http://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: SMAH1 http://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/#comment-19170 SMAH1 Mon, 21 Feb 2011 18:24:27 +0000 http://www.xaprb.com/blog/?p=10#comment-19170 Hi

Thank you for useful articles.
But there is problem!

in below sample (in SQL SERVER):

CREATE TABLE [Test] (
[ID] [bigint] IDENTITY (1, 1) NOT NULL ,
[Date] [char] (10),
[B] [int],
[C] [int]
)
INSERT INTO [TEST].[Test]
VALUES (’1389/01/01′,’1′,’1′)
INSERT INTO [TEST].[Test]
VALUES (’1389/01/01′,’5′,’2′)
INSERT INT[TEST].[Test]
VALUES (’1389/01/02′,’2′,’10′)
INSERT INT[TEST].[Test]
VALUES (’1389/01/02′,’2′,’11′)
INSERT INT[TEST].[Test]
VALUES (’1389/01/03′,’2′,’2′)
INSERT INT[TEST].[Test]
VALUES (’1389/01/03′,’5′,’1′)
INSERT INT[TEST].[Test]
VALUES (’1389/01/01′,’2′,’1′)
INSERT INT[TEST].[Test]
VALUES (’1389/01/02′,’2′,’12′)
INSERT INT[TEST].[Test]
VALUES (’1389/01/03′,’2′,’2′)

when we use:

select l.Date, l.B, l.C, count(*) as num
from Test as l
left outer join Test as r
on l.Date = r.Date
and l.B >= r.B
group by l.Date, l.B, l.C

we have:


1389/01/02 2 10 3
1389/01/02 2 11 3
1389/01/02 2 12 3

But must be create like :


1389/01/02 2 10 1
1389/01/02 2 11 2
1389/01/02 2 12 3

No my question:
What solve it? (Create above table)?
Note : I want use SQL only (No use TSQL)!
Note 2 : Cause problem is ‘Like Data in Date and B Field’

Description 1: My english is poor.Excuse me!
Description 2: Date is persian calender!

]]>
By: Brian M http://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/#comment-18652 Brian M Tue, 14 Sep 2010 15:43:39 +0000 http://www.xaprb.com/blog/?p=10#comment-18652 If you want a simple way to reproduce the row_number without grouping its easily achieved with a user variable.

SELECT *, @rownum:=@rownum+1 rownum, (SELECT @rownum:=0) dummy
FROM fruits
HAVING rownum <= (pg*lim);

You'll notice I used it for dynamic paging of the result set in my stored proc.

]]>
By: Zeilen in MySQL enumerieren Lars seine Seite http://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/#comment-18034 Zeilen in MySQL enumerieren Lars seine Seite Mon, 15 Mar 2010 23:14:06 +0000 http://www.xaprb.com/blog/?p=10#comment-18034 [...] Eine weitere Möglichkeit beschreibt Xarpb in seinem Artikel How to simulate the SQL ROW_NUMBER function. [...]

]]>
By: Shiva M http://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/#comment-17818 Shiva M Mon, 15 Feb 2010 23:28:34 +0000 http://www.xaprb.com/blog/?p=10#comment-17818 In my above comment “not equal to” with greater than and less than got gobbled up. Hence replaced it with “!=”.

SET @n := 0;
SET @t := NULL;
SELECT f.*, @n
FROM fruits f
WHERE CASE WHEN f.type != @t THEN @n := 0 ELSE 1 END >= 0
AND(@n := @n + 1) >= 0
AND(@t := f.type) IS NOT NULL

]]>
By: Shiva M http://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/#comment-17817 Shiva M Mon, 15 Feb 2010 23:21:12 +0000 http://www.xaprb.com/blog/?p=10#comment-17817 One more solution,

SET @n := 0;
SET @t := NULL;
SELECT f.*, @n
FROM fruits f
WHERE CASE WHEN f.type @t THEN @n := 0 ELSE 1 END >= 0
AND(@n := @n + 1) >= 0
AND(@t := f.type) IS NOT NULL

]]>