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 |
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2007-11-19 : 02:31:26
|
| Table ID code Name1 10 a1 20 a1 30 a2 10 b2 50 b2 30 b2 20 b3 40 c4 10 d4 30 d desired output 1 *** a2 *** b3 40 c4 *** di need to write a single select statement, so that when there are multiple codes for 1 IDthe in output the need to replace it with "***" and if there is only one code for the idthen i need to display the code in the result |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-19 : 03:30:43
|
| Where do you want to display data?Return data with count and when you show check if count exceeds 1 and display *** accordinglyMadhivananFailing to plan is Planning to fail |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2007-11-19 : 03:37:07
|
| For table structure like belowcreate table #temp( id int, code int, [Name] varchar(10))try this queryselect Id, [Name], Case when ((select count(code) from #temp t1 where t1.Name=t2.name) >1) then '***' else (select cast(code as varchar) from #temp t1 where t1.Name=t2.name ) End from #temp t2 group by [Name],Id |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-19 : 03:41:03
|
As this is posted in SQL Server 2005, this should workselect id,max(case when counting=1 then cast(code as varchar(10)) else '***' end),max(name) as name from( select *,count(*) over(partition by id) as counting from table) as tgroup by id But as I said in the first reply try to do this in front end applicationMadhivananFailing to plan is Planning to fail |
 |
|
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2007-11-19 : 04:00:54
|
| i display the result on a excel report ,so i cant do any changes in front end |
 |
|
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2007-11-19 : 06:51:44
|
quote: Originally posted by madhivanan As this is posted in SQL Server 2005, this should workselect id,max(case when counting=1 then cast(code as varchar(10)) else '***' end),max(name) as name from( select *,count(*) over(partition by id) as counting from table) as tgroup by id But as I said in the first reply try to do this in front end applicationMadhivananFailing to plan is Planning to fail
thanks a lot Madhivanan this query worked perfectly in 2005is there an equivalent for the "partition" in 2000..? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-19 : 07:09:39
|
| <<is there an equivalent for the "partition" in 2000..?>>No. You need other methods such aggregated subqueries,etcMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|