Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to columns ...
prid isroi
12 y
12 N
13 n
14 y
15 n
15 n

if isroi has atleast one y for prid then it has to display 'y' or 'N'

Output will be

prid isroi new_roi
12 y y
12 N y
13 n n
14 y y
15 n n
15 n n
Posted
Comments
Murali Vijay 17-Jun-14 3:23am    
select prid,new_roi=case when isroi='y' then 'y' when isroi='N' then 'y' else '' end from table_name;

try this.. :)

SQL
;with samp as(
  select count(*)as CountPrid,prid   from TableName where isroi='y' group by prid 
  )
  SELECT [prid]
      ,[isroi]
	  ,case (select CountPrid from samp where samp.prid=TableName.prid) when 1 then 'Y' ELSE 'N' END as prids
  FROM TableName
 
Share this answer
 
Is this SQL Server?
If so, I think it can be solved neatly using a Common Table Expression (CTE)...
we create a temporary table with your sample data:
SQL
create table #tmp (prid int, isroi varchar(1));

insert into #tmp select 12,'y';
insert into #tmp select 12,'N';
insert into #tmp select 13,'n';
insert into #tmp select 14,'y';
insert into #tmp select 15,'n';
insert into #tmp select 15,'n';

select * from #tmp;

we know we want a recordset which collects all the "prid" where a 'y' value for "isroi" is present:
SQL
select distinct prid,isroi from #tmp where isroi = 'y'

We put this query in a CommonTableExpression, and LEFT OUTER JOIN it to a SELECT * FROM the original table, using ISNULL(,) to replace the missing values with an 'n'
SQL
with CTEYesValues as (select distinct prid,isroi from #tmp where isroi = 'y')

select #tmp.*, isnull(CTEYesValues.isroi, 'n') as new_roi from #tmp left outer join CTEYesValues on CTEYesValues.prid = #tmp.prid;

The output:
prid	isroi	new_roi
12	y	y
12	N	y
13	n	n
14	y	y
15	n	n
15	n	n
 
Share this answer
 
ok here it is my solution
SQL
select prid,isroi, max(isroi) over(partition by prid) as new_roi
from tmp

if you have only y and n values in isroi column
then my solution will perfectly work for you

My Fiddle solution
 
Share this answer
 
try this

select prid,new_roi=case when isroi='y' then 'y' when isroi='N' then 'y' else '' end from table_name;
 
Share this answer
 
Comments
Nirav Prabtani 17-Jun-14 3:26am    
Read question care fully, try to understand, it is not simple case when.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900