SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update column with default values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

maevr
Posting Yak Master

Sweden
169 Posts

Posted - 02/27/2013 :  03:55:22  Show Profile  Reply with Quote
I need help writing a query that set a value to Yes when the value is 'priv'. The table can contain multiple rows with the same fnr, but when the fnr contains 'priv' the value should be set to 'Yes'
When at least one fnr contains 'priv' in column pnr then a returning value of 'Yes' should be displayed otherwise 'No'

Example:
create table #source
(
fnr nvarchar(10)
,pnr nvarchar(10) null
)

insert #source(fnr, pnr) values('11111','priv')
insert #source(fnr, pnr) values('11111','off')
insert #source(fnr, pnr) values('22222','priv')
insert #source(fnr, pnr) values('33333','priv')
insert #source(fnr, pnr) values('44444','off')
insert #source(fnr, pnr) values('55555','off')
insert #source(fnr, pnr) values('55555','priv')

Expected output:
fnr newColumn
11111 Yes
22222 Yes
33333 Yes
44444 No
55555 Yes

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/27/2013 :  04:00:55  Show Profile  Reply with Quote

SELECT fnr
WHEN MAX(CASE WHEN pnr='priv' THEN 1 ELSE 0 END)=1 THEN 'Yes' ELSE 'No' END
FROM table
GROUP BY fnr


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Ranjit.ileni
Posting Yak Master

India
182 Posts

Posted - 02/28/2013 :  07:18:25  Show Profile  Reply with Quote
added missed CASE clause

quote:
Originally posted by visakh16


SELECT fnr,
CASE WHEN MAX(CASE WHEN pnr='priv' THEN 1 ELSE 0 END)=1 THEN 'Yes' ELSE 'No' END
FROM table
GROUP BY fnr


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





--Irk
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000