Unix Technical Forum

Easy newbie question

This is a discussion on Easy newbie question within the SQL Server forums, part of the Microsoft SQL Server category; --> Sorry if this seems too easy to be interesting - I have yet to find an elegant solution, as ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:43 PM
jrpfinch
 
Posts: n/a
Default Easy newbie question

Sorry if this seems too easy to be interesting - I have yet to find an
elegant solution, as I am completely new at this so don't really know
what to look for. I have the following table:

Id Cat
1 A
2 B
3 C
4 A
5 B
6 B
7 C
....

I would like to create a new column, CatId, which has a value n, which
is the nth appearance of the record's category (ordered by Id). In
this case it would be 1,1,1,2,2,3,2 because e.g. Id=6 is the third
appearance of the letter B so it would equal 3.

How could I do this in T-SQL?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:43 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Easy newbie question

On 19 Mar 2007 11:26:36 -0700, jrpfinch wrote:

>Sorry if this seems too easy to be interesting - I have yet to find an
>elegant solution, as I am completely new at this so don't really know
>what to look for. I have the following table:
>
>Id Cat
>1 A
>2 B
>3 C
>4 A
>5 B
>6 B
>7 C
>...
>
>I would like to create a new column, CatId, which has a value n, which
>is the nth appearance of the record's category (ordered by Id). In
>this case it would be 1,1,1,2,2,3,2 because e.g. Id=6 is the third
>appearance of the letter B so it would equal 3.
>
>How could I do this in T-SQL?


Hi jrpfinch,

SELECT a.Id, a.Cat,
COUNT(*) AS CatId
FROM YourTable AS a
INNER JOIN YourTable AS b
ON b.Cat = a.Cat
AND b.Id <= a.Id
GROUP BY a.Id, a.Cat;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:43 PM
David Portas
 
Posts: n/a
Default Re: Easy newbie question

On 19 Mar, 18:26, "jrpfinch" <jrpfi...@gmail.com> wrote:
> Sorry if this seems too easy to be interesting - I have yet to find an
> elegant solution, as I am completely new at this so don't really know
> what to look for. I have the following table:
>
> Id Cat
> 1 A
> 2 B
> 3 C
> 4 A
> 5 B
> 6 B
> 7 C
> ...
>
> I would like to create a new column, CatId, which has a value n, which
> is the nth appearance of the record's category (ordered by Id). In
> this case it would be 1,1,1,2,2,3,2 because e.g. Id=6 is the third
> appearance of the letter B so it would equal 3.
>
> How could I do this in T-SQL?


To ensure you get a good answer to a question like this always state
what version you are using and include a CREATE TABLE statement that
defines the key(s) in your table.

Here's my guess based on the assumption that (Id, Cat) is unique. Note
that if CatID is always to be based on the values of the other two
columns then it might make more sense NOT to include it in the table.
Instead compute it whenever the results are displayed.

UPDATE tbl
SET CatID =
(SELECT COUNT(*)
FROM tbl AS t
WHERE Cat = tbl.Cat
AND ID <= tbl.ID);

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:18 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com