| Author |
Topic |
|
nmaruti
Starting Member
10 Posts |
Posted - 2009-02-19 : 19:14:54
|
| I have a table with the following data with two columns Srl# and Name1 Mary2 David3 Jhonson4 Dextor5 Jenifer6 David7 David8 Jenifer9 Jenifer10 Jenifer11 Jhonson12 Jhonson13 Jhonson14 Jhonson15 MaryI want to a query which outputs the data from above table where if the consecutive Srl#s having same Name will be marked as ^ after 1st occurance. For the above data the output should be1 Mary2 David3 Jhonson4 Dextor5 Jenifer6 David7 ^8 Jenifer9 ^10 ^11 Jhonson12 ^13 ^14 ^15 MaryThanks,Narendra. |
|
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2009-02-19 : 19:36:38
|
| Here's an example: --Create some test values Create table #names(nameid int, nameval varchar(20)) insert into #names(nameid,nameval) values(1,'mike'),(2,'Mike'),(3,'Joe'),(4,'John') --Use a CTE to hold the min value for each name ;with minVals(minid,nameval) as ( select min(nameid) as minid,nameval from #names group by nameval ) select case when cast(v.minid as varchar(20)) IS NULL then '^' else cast(n.nameid as varchar(20)) end,n.nameval from #names n left join minVals v on n.nameid=v.minidI should note here that if this is a large table you might want to do this with a subquery instead of a CTE as the CTE is stored in memory and a large set of data would decrease performance.Mike"oh, that monkey is going to pay" |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-19 : 19:41:15
|
| [code]Select t.[Srl#],(Case When t.[Name] = m.[Name then '^' Else t.[Name]End) from table t left outer join table mon m.[Srl#]= t.[Srl#] +1[/code] |
 |
|
|
jbp_j
Starting Member
24 Posts |
Posted - 2009-02-20 : 04:21:57
|
| hi try this one also.update t1 set t1.name = '^'from @t as t1inner join @t as t2 on t1.id = t2.id+1where t1.name = t2.name select * from @t |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-20 : 06:30:54
|
I guess he is not looking for update query.quote: Originally posted by jbp_j hi try this one also.update t1 set t1.name = '^'from @t as t1inner join @t as t2 on t1.id = t2.id+1where t1.name = t2.name select * from @t
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-02-20 : 07:38:38
|
quote: Originally posted by nmaruti I have a table with the following data with two columns Srl# and Name1 Mary2 David3 Jhonson4 Dextor5 Jenifer6 David7 David8 Jenifer9 Jenifer10 Jenifer11 Jhonson12 Jhonson13 Jhonson14 Jhonson15 MaryI want to a query which outputs the data from above table where if the consecutive Srl#s having same Name will be marked as ^ after 1st occurance. For the above data the output should be1 Mary2 David3 Jhonson4 Dextor5 Jenifer6 David7 ^8 Jenifer9 ^10 ^11 Jhonson12 ^13 ^14 ^15 MaryThanks,Narendra.
This is formation issue. If you use front end application, do it thereMadhivananFailing to plan is Planning to fail |
 |
|
|
nmaruti
Starting Member
10 Posts |
Posted - 2009-02-20 : 11:56:05
|
quote: Originally posted by sodeep
Select t.[Srl#],(Case When t.[Name] = m.[Name then '^' Else t.[Name]End) from table t left outer join table mon m.[Srl#]= t.[Srl#] +1
Thanks Sodeep. It works. |
 |
|
|
nmaruti
Starting Member
10 Posts |
Posted - 2009-02-20 : 11:56:47
|
| Thanks Sodeep. The query you have given worked fine for me.. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-20 : 12:07:27
|
quote: Originally posted by nmaruti Thanks Sodeep. The query you have given worked fine for me..
Good but did you see reply from Madhi. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-20 : 12:07:40
|
quote: Originally posted by nmaruti Thanks Sodeep. The query you have given worked fine for me..
did you see Madhi's suggestion? did you consider doing this at front end? |
 |
|
|
|