| Author |
Topic |
|
artbishop
Starting Member
18 Posts |
Posted - 2009-02-18 : 22:14:31
|
| How do i get the following result:RANKID NAMEID NAME0 1 Alfred1 2 Jen2 5 Amber2 3 Maricel1 4 Philipif I have two SQL database table:first table is dbo.NAMEID NAME1 Alfred2 Jen3 Maricel4 Philip5 Amberthen second table is RANKNAMEID RANKID1 02 13 24 15 2 thanks |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-18 : 22:17:26
|
| Learn JOINS in SQL Server. Can we see what you tried? |
 |
|
|
artbishop
Starting Member
18 Posts |
Posted - 2009-02-18 : 22:21:44
|
| SELECT TOP 2 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDUNION ALLSELECT TOP 2 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDWHERE dbo.T_Relationships.NAMEID = 5UNION ALLSELECT TOP 1 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDWHERE dbo.T_Relationships.PARENT_NAMEID = 2UNION ALLSELECT TOP 1 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDWHERE dbo.T_Relationships.NAMEID = 4 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-18 : 22:24:45
|
quote: Originally posted by artbishop SELECT TOP 2 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDUNION ALLSELECT TOP 2 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDWHERE dbo.T_Relationships.NAMEID = 5UNION ALLSELECT TOP 1 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDWHERE dbo.T_Relationships.PARENT_NAMEID = 2UNION ALLSELECT TOP 1 dbo.T_Relationships.PARENT_NAMEID AS LEVEL, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names LEFT JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEIDWHERE dbo.T_Relationships.NAMEID = 4
OMG what is this? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-18 : 22:38:05
|
| Go through this:http://www.w3schools.com/sql/default.asp |
 |
|
|
artbishop
Starting Member
18 Posts |
Posted - 2009-02-18 : 22:45:02
|
| I am having problem arranging the rankid from 0 1 2 2 1...wherein the first number 2 should be Amber instead of Maricel...RANKID NAMEID NAME0 1 Alfred1 2 Jen2 5 Amber2 3 Maricel1 4 Philip |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-18 : 23:08:15
|
| [code]Select RankID,NAMEID,[Name]from(SelectROW_NUMBER() Over(partition by RankID order by NAME)as seq,M.RankID,T.NAMEID,T.[Name] from Table1 Tinner join Table2 MOn M.id = T.id)Zorder by seq[/code] |
 |
|
|
artbishop
Starting Member
18 Posts |
Posted - 2009-02-18 : 23:41:11
|
| I followed the formula:SELECT dbo.T_Relationships.PARENT_NAMEID, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM (SELECT ROW_NUMBER() OVER (partition BY dbo.T_Relationships.PARENT_NAMEID ORDER BY NAME) AS seq, dbo.T_Relationships.PARENT_NAMEID, dbo.T_Relationships.NAMEID, dbo.T_Names.NAMEFROM dbo.T_Names INNER JOIN dbo.T_Relationships ON dbo.T_Names.ID = dbo.T_Relationships.NAMEID) ZORDER BY seqnow i am getting the error message: [Microsoft\[ODBC SQL Server Driver][SQL SERVER]'ROW_NUMBER' is not a recognized function name...any idea? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-18 : 23:45:39
|
| r u using 2000 r 2005 ? |
 |
|
|
artbishop
Starting Member
18 Posts |
Posted - 2009-02-18 : 23:49:02
|
| 2000 |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-18 : 23:51:51
|
| in 2000 u can't use the row_number function |
 |
|
|
artbishop
Starting Member
18 Posts |
Posted - 2009-02-18 : 23:53:07
|
| any idea to solve the problem? |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-19 : 00:37:28
|
| [code]declare @Name Table ( ID int, NAME varchar(32))insert into @name select 1, 'Alfred' union allselect 2 ,'Jen' union allselect 3 ,'Maricel' union allselect 4 ,'Philip' union allselect 5 ,'Amber'declare @rank table (NAMEID int, RANKID int)insert into @rankselect 1, 0 union allselect 2, 1 union allselect 3, 2 union allselect 4, 1 union allselect 5, 2declare @cnt Intselect @cnt = count(*) from @nameselect r.rankid,n.id,n.namefrom @name ninner join @rank r on r.nameid = n.id order by case when id = (select max(id) from @name) then id-((@cnt/2)+1) else id end[/code] |
 |
|
|
artbishop
Starting Member
18 Posts |
Posted - 2009-02-19 : 00:59:32
|
| thanks.. =) |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-19 : 01:04:42
|
| Welcome |
 |
|
|
|