DBA12345 (9/11/2013)
Hi I want to update Flag column in second table based on the Adder names.
If the Applicatiion has atleast one AIX and Adder name is UDB then the flag would be True.
If the Application has more the one AIX and Adder names are diferent then the flag would be null.
APpName OS Adder
App1 ||| Windows|||Null
App1 ||| Linux |||UDB
App1 ||| AIX |||UDB
App1 ||| Linux |||Sql
App2 ||| AIX ||| UDB
App2 ||| Windows||| UDB
App2 ||| Linux ||| UDB
App2 ||| AIX ||| UDB
OUTPUT SHOULD BE LOOK LIKE BELOW
APpName OS Adder Flag
App1||| Windows|||Null|||null
App1||| Linux |||UDB |||null
App1||| AIX |||UDB |||null
App1||| Linux |||Sql |||null
App2|||AIX ||| UDB|||TRUE
App2|||Windows||| UDB|||TRUE
App2|||Linux ||| UDB|||TRUE
App2|||AIX ||| UDB|||TRUE
Let me know fi you need addiitional information.
Thanks
Aswin
I am not sure which is correct, your explanation or your desired output. The stated rules, the sample data and the output you said you wanted don't match up.
If the Applicatiion has atleast one AIX and Adder name is UDB then the flag would be True.
In your output you have App1 as NULL but your rule says it should be "True".
If the Application has more the one AIX and Adder names are diferent then the flag would be null.
App2 has 2 rows for AIX but the Adder values are the same. According to the rules shouldn't this also be "True".
I cobbled together something resembling ddl and sample data in a format you should post with in the future.
create table #SomeTable
(
AppName char(4),
OS varchar(10),
Adder char(3)
)
insert #SomeTable
select 'App1', 'Windows', Null union all
select 'App1', 'Linux', 'UDB' union all
select 'App1', 'AIX ', 'UDB' union all
select 'App1', 'Linux', 'Sql' union all
select 'App2', 'AIX', 'UDB' union all
select 'App2', 'Windows', 'UDB' union all
select 'App2', 'Linux', 'UDB' union all
select 'App2', 'AIX', 'UDB'
Now to setup some data that meets the requirements of rule #2 I added some more data.
insert #SomeTable
select 'App3', 'AIX', 'UDB' union all
select 'App3', 'Windows', 'UDB' union all
select 'App3', 'Linux', 'UDB' union all
select 'App3', 'AIX', 'xxx'
OK. Now we have something to work with. If I understand your requirements I think you want something along these lines.
select *, case MyCount when 1 then 'True' end
from #SomeTable s
cross apply (select COUNT(distinct Adder) as MyCount from #SomeTable s2 where s2.AppName = s.AppName and s2.OS = 'AIX') x
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/