Connecting Tech Pros Worldwide Help | Site Map

Row Numbers for a View

  #1  
Old March 28th, 2006, 12:45 AM
Andrew Lias
Guest
 
Posts: n/a
I've been given a task that I believe is, basically, impossible, but
I'd like to see if there's a way to do it.

What my boss wants me to do is to create a view, in SQL Server 2000,
that will provide not only a row number field of some sort, but that
will produce sequential ordering for arbitrary selects and orderings.
So, if my data is a table with values from A thru D and my user does
SELECT data FROM vwTable, the result would be:

Row Data
--- ----
1 A
2 B
3 C
4 D

But is they did SELECT data FROM vwTable ORDER BY data DSC, they would
get

Row Data
--- ----
1 D
2 C
3 B
4 A

And if the did SELECT data FROM vwTable WHERE Data IN ('B', 'C'), they
would get

Row Data
--- ----
1 B
2 C

In SQL 2005, of course, this would be fairly trivial since I could use
the ROW_NUMBER function. In 2000, though, it seems to be utterly
impossible. My boss, however, is convinced that there must be some way
to create a calculated field to do it.

I'll be cursed if I can figure out a way to do so.

Any suggestions would be appreciated.

  #2  
Old March 28th, 2006, 02:35 AM
Anith Sen
Guest
 
Posts: n/a

re: Row Numbers for a View


>> In 2000, though, it seems to be utterly impossible. My boss, however, is[color=blue][color=green]
>> convinced that there must be some way to create a calculated field to do
>> it.[/color][/color]

Paste the following in Google search box:
"dynamically number rows site:support.microsoft.com"

--
Anith


  #3  
Old March 28th, 2006, 01:45 PM
Madhivanan
Guest
 
Posts: n/a

re: Row Numbers for a View


Where do you want to show the data?
Use Front End application to do this

Madhivanan

  #4  
Old March 28th, 2006, 03:25 PM
Andrew Lias
Guest
 
Posts: n/a

re: Row Numbers for a View



Anith Sen wrote:[color=blue][color=green][color=darkred]
> >> In 2000, though, it seems to be utterly impossible. My boss, however, is
> >> convinced that there must be some way to create a calculated field to do
> >> it.[/color][/color]
>
> Paste the following in Google search box:
> "dynamically number rows site:support.microsoft.com"[/color]

Thanks, however, while that is a good way to derive row numbers in a
select statement, unfortunately it isn't quite what my boss is asking
me to do. She wants a view that will produce row counts in a
calculated field regardless of the order that the user uses to select
the data.

I would prefer to require the user to generate the row numbers in their
selects, wjhich wouldd allow for the solution you offered.
Unfortunately, that isn't what I've been tasked to do.

  #5  
Old March 28th, 2006, 03:25 PM
Andrew Lias
Guest
 
Posts: n/a

re: Row Numbers for a View



Madhivanan wrote:[color=blue]
> Where do you want to show the data?
> Use Front End application to do this[/color]

SQL Reporting Services.

  #6  
Old March 28th, 2006, 07:35 PM
Hugo Kornelis
Guest
 
Posts: n/a

re: Row Numbers for a View


On 27 Mar 2006 16:32:09 -0800, Andrew Lias wrote:
[color=blue]
>I've been given a task that I believe is, basically, impossible, but
>I'd like to see if there's a way to do it.
>
>What my boss wants me to do is to create a view, in SQL Server 2000,
>that will provide not only a row number field of some sort, but that
>will produce sequential ordering for arbitrary selects and orderings.
>So, if my data is a table with values from A thru D and my user does
>SELECT data FROM vwTable, the result would be:
>
>Row Data
>--- ----
>1 A
>2 B
>3 C
>4 D
>
>But is they did SELECT data FROM vwTable ORDER BY data DSC, they would
>get
>
>Row Data
>--- ----
>1 D
>2 C
>3 B
>4 A
>
>And if the did SELECT data FROM vwTable WHERE Data IN ('B', 'C'), they
>would get
>
>Row Data
>--- ----
>1 B
>2 C
>
>In SQL 2005, of course, this would be fairly trivial since I could use
>the ROW_NUMBER function. In 2000, though, it seems to be utterly
>impossible. My boss, however, is convinced that there must be some way
>to create a calculated field to do it.
>
>I'll be cursed if I can figure out a way to do so.
>
>Any suggestions would be appreciated.[/color]

Hi Andrew,

The way you describe it here, it's impossible. That holds true for both
SQL Server 2005 and SQL Server 2000. Even ROW_NUMBER() won't help you.

If you need the row numbers to match the order specifiede on the select
and if you want to skip numbers for rows not included in the select,
you'll have to add row numbering logic on the SELECT statement. If you
add row numbers in the view, the numbers won't change if you exclude
some rows or choose a different order when selecting from the view.

Just to prevent misunderstanding - it is NOT impossible to get the
result sets you require. But it's only possible by extending the SELECT
with some row numbering logic. Either using ROW_NUMBER() if you're using
SQL Server 2005, or by using either a correlated subquery or a self-join
and a GROUP BY if you're using SQL Server 2000.

--
Hugo Kornelis, SQL Server MVP
  #7  
Old March 28th, 2006, 10:25 PM
Erland Sommarskog
Guest
 
Posts: n/a

re: Row Numbers for a View


Andrew Lias (anrwlias@gmail.com) writes:[color=blue]
> Thanks, however, while that is a good way to derive row numbers in a
> select statement, unfortunately it isn't quite what my boss is asking
> me to do. She wants a view that will produce row counts in a
> calculated field regardless of the order that the user uses to select
> the data.[/color]

Time to get a new boss?

What she is asking for is not possible. You would have to package the
user's SELECT statement somehow, so you can modify to add the row-number
column. As Hugo pointed out, this is the same on SQL 2005.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  #8  
Old March 29th, 2006, 02:55 PM
Madhivanan
Guest
 
Posts: n/a

re: Row Numbers for a View


>>SQL Reporting Services

Cant you make use of Recordnumber feature such as the one available in
Crystal reports?

Madhivanan

  #9  
Old March 30th, 2006, 04:15 PM
Andrew Lias
Guest
 
Posts: n/a

re: Row Numbers for a View



Erland Sommarskog wrote:[color=blue]
> Andrew Lias (anrwlias@gmail.com) writes:[color=green]
> > Thanks, however, while that is a good way to derive row numbers in a
> > select statement, unfortunately it isn't quite what my boss is asking
> > me to do. She wants a view that will produce row counts in a
> > calculated field regardless of the order that the user uses to select
> > the data.[/color]
>
> Time to get a new boss?
>
> What she is asking for is not possible. You would have to package the
> user's SELECT statement somehow, so you can modify to add the row-number
> column. As Hugo pointed out, this is the same on SQL 2005.[/color]

That's what I thought. I just wanted to be extra sure that there
wasn't some tricky way to do this before I went back to her and said
that it simply could not be done the way that she was asking.

  #10  
Old April 6th, 2006, 07:05 PM
Doug
Guest
 
Posts: n/a

re: Row Numbers for a View


if you can use a stored procedure instead of a view, you could select
the data INTO a temp table in the "correct order", alter the table to
add an identity column, and return that ordered by identity.
before someone gets excited, there isn't a GUARANTEE this will work
forever in future versions of SQL, but it probably will.

  #11  
Old April 6th, 2006, 11:05 PM
Erland Sommarskog
Guest
 
Posts: n/a

re: Row Numbers for a View


Doug (drmiller100@hotmail.com) writes:[color=blue]
> if you can use a stored procedure instead of a view, you could select
> the data INTO a temp table in the "correct order", alter the table to
> add an identity column, and return that ordered by identity.
> before someone gets excited, there isn't a GUARANTEE this will work
> forever in future versions of SQL, but it probably will.[/color]

There is no guarantee that it will work any version of SQL Server. In fact
for a result set of any size, I would not expect it to work.

What is guaranteed to work, at least in SQL 2005, is if you have a
table with an IDENTITY table, and perform an INSERT with an ORDER BY.

Note that this does not apply to SELECT INTO with the IDENTITY function
and ORDER BY. In that case, there is *no* guarantee.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  #12  
Old April 7th, 2006, 04:35 PM
Doug
Guest
 
Posts: n/a

re: Row Numbers for a View


Your answer is better - have the identity already there.

But, at least we "solved" the problem!!!!

  #13  
Old May 12th, 2006, 10:54 AM
Newbie
 
Join Date: May 2006
Posts: 1

re: Row Numbers for a View


Hey Andrew

Nothing is impossible, maybe I have read too fast but here is how I would do it.
Sounds like your boss just wants row numbering on your result set.
In reporting services use this expression

=RowNumber("DataSetName")

That would be like using Crystal's RecordNumber

Hope this helps
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
any way to jump the cursor to a specific field (variable) while inform view? mckbill answers 3 December 13th, 2007 05:25 PM
"filtered view" upon lists? Wildemar Wildenburger answers 10 September 13th, 2006 01:55 AM
Record number or row number answers 1 March 19th, 2006 04:55 PM
ListView row numbers? Andrew S. Giles answers 1 November 17th, 2005 10:45 AM