Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get the row position in the group?

Author  Topic 

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2010-08-21 : 22:37:34
Hello!

I have two tables:

    Unit:
UnitId int PK
Title varchar

UnitOption:
UnitOptionId int PK
UnitId int FK
Title varchar

Quote:
QuoteId int PK
UnitOptionId int FK
Title varchar


I want to create a scalar UDF that takes a QuoteId param and returns a varchar that contains the following description (pseudu):

    Quote.Title + '-' + Unit.Title + '-' + Unit.UnitId + 
/* Here is where my question is:
If there are more than 1 UnitOption under this Unit, then
return '-' + the UnitOption number under this Unit
(i.e.) if under this Unit, there are 3 UnitOption with IDs 13, 17, 55
under the unit, and the current Quote.UnitOptionId is the 17 one,
it should return 2.
Which means I want to retrieve an ID of this row in the group.
Else
return ''
*/


Shimmy

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-22 : 07:14:28
can you post some sample data, table definitions and resired outoput.

thank you
Go to Top of Page

weitzhandler
Yak Posting Veteran

64 Posts

Posted - 2010-08-22 : 09:39:53
I found the answer myself.

It's the [url http://msdn.microsoft.com/en-us/library/ms186734.aspx]ROW_NUMBER()[/url] function, then, using a [url http://msdn.microsoft.com/en-us/library/ms175972.aspx]WITH[/url] statement and a [url http://msdn.microsoft.com/en-us/library/ms181765.aspx]CASE[/url] statement I make up my varchar result.


Shimmy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-22 : 12:15:12
quote:
Originally posted by weitzhandler

I found the answer myself.

It's the [url http://msdn.microsoft.com/en-us/library/ms186734.aspx]ROW_NUMBER()[/url] function, then, using a [url http://msdn.microsoft.com/en-us/library/ms175972.aspx]WITH[/url] statement and a [url http://msdn.microsoft.com/en-us/library/ms181765.aspx]CASE[/url] statement I make up my varchar result.


Shimmy


you can post your solution if you feel somebody else will also benefit out of it!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -