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.
Author |
Topic |
Jazzinfour
Starting Member
3 Posts |
Posted - 2011-05-15 : 23:43:12
|
I have been assigned a task to create a select statement to return data from two tables like these: Table1 : Name, Rank, Value Bob 12 121 Bob 15 121 Fred 10 132 Fred 11 132 Fred 16 132Table2 : Name, Value, Position Bob 121 Manager Fred 132 AssistantThe desired result would be:Result : Name, Rank, Value, Position Bob 12 121 Manager Bob 15 121 Fred 10 132 Assistant Fred 11 132 Fred 16 132Currently I can only get this with an inner join:Result : Name, Rank, Value, Position Bob 12 121 Manager Bob 15 121 Manager Fred 10 132 Assistant Fred 11 132 Assistant Fred 16 132 AssistantHow can I return either a null or blank for every row but the first? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-05-15 : 23:50:08
|
that is more of a presentation issue. You should do this in your front end application KH[spoiler]Time is always against us[/spoiler] |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-05-16 : 09:41:18
|
This is known as Suppress if duplicated feature that should be done in the front end applicationMadhivananFailing to plan is Planning to fail |
|
|
Jazzinfour
Starting Member
3 Posts |
Posted - 2011-05-16 : 10:05:45
|
Yes, yes, I know, I would normally do this in the front-end but in this case I do not have access to source code for the front end, only an available field to use for the column data. If this were a simple report or .Net or VB.... I'd have it solved already. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-05-16 : 10:16:21
|
Post your full codeMadhivananFailing to plan is Planning to fail |
|
|
MSquared
Yak Posting Veteran
52 Posts |
Posted - 2011-05-16 : 10:30:22
|
Try thisdeclare @Table1 table(Name varchar(10), Ranking tinyint, Value tinyint)insert into @Table1select 'Bob',12, 121 union allselect 'Bob', 15, 121 union all select 'Fred', 10, 132 union allselect 'Fred', 11, 132 union allselect 'Fred', 16, 132declare @Table2 table(Name varchar(10), Value tinyint, Position varchar(15))insert into @Table2select 'Bob', 121, 'Manager' union allselect 'Fred', 132, 'Assistant';with cte as(select a.name, a.ranking, a.value, b.Position, ROW_NUMBER() over (PARTITION by a.value order by a.value, a.Ranking) RowNum from @Table1 a inner join @Table2 b on a.value = b.value) select Name, Ranking, Value, case when RowNum > 1 then '' else Position end Position from cteFor Faster results please follow the posting guidelines herehttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
|
|
Jazzinfour
Starting Member
3 Posts |
Posted - 2011-05-16 : 12:41:29
|
MSquared, That's the ticket! Much appreciated. |
|
|
MSquared
Yak Posting Veteran
52 Posts |
Posted - 2011-05-16 : 14:16:20
|
Glad that worked for you, but keep in mind, this sort of thing should really be done in the presentation tier and not in the database as the others have statedFor Faster results please follow the posting guidelines herehttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
|
|
|
|
|
|
|