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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help in a create a view with CASE with a select...

Author  Topic 

leoc50
Yak Posting Veteran

54 Posts

Posted - 2013-05-09 : 14:24:49
Hi All,

I'm hoping I can get some help/hints here. Basically I need to create a view based on a regular select from one table including a CASE (or whichever T-Sql feature applies better) stmt which select from another query...I've tried a couple of scenarios but no luck....
-- this is a non-working t-sql scenarion to show the needs:

select
server_id, Server_Name, [Application ID], [Application Name], [DB Name],
ApplicationInstanceID, ApplicationInstanceName
ApplicationID =
CASE
(when [a.[application id] = b.applicationId] then 'Yes' --> this would come from something like "select ?? from Table1 a, Table2 b where a.[application id] = b.applicationId)
(when [a.[application id] != b.applicationId] then NULL --> this would come from something like "select ?? from Table1 a, Table2 b where a.[application id] not in b.applicationId)
END
from Table1 a
order by server_name, [db name]

Thanks in advance for any help/hint!!!

Sincerely,
Leo

- lec

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-09 : 15:07:41
You probably need to join the two tables and then apply the CASE expression. However, I am not able to discern what you are looking for based on what you posted. If you can post some sample data long with table schema and the output you are looking for, that would make it easier to understand.
Go to Top of Page

leoc50
Yak Posting Veteran

54 Posts

Posted - 2013-05-09 : 15:16:22
Basically they want all columns [say] from table1 and and if [aplication id] from table1 matches applicationid from table2 then add to the list/result a column saying 'YES', if they don't match leave the value on that column as NULL.

Table2 is basically a look-up table; in this case for the applicationId of table1.

Hope this helps!!

- lec
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-05-10 : 14:35:25
[CODE]select
server_id, Server_Name, [Application ID], [Application Name], [DB Name],
ApplicationInstanceID, ApplicationInstanceName
ApplicationID =
CASE
when b.applicationId is not null then 'Yes' else NULL end
from Table1 a
left outer join
Table2 b
on a.[application id] = b.applicationId]
order by server_name, [db name][/CODE]Maybe?

=================================================
There are two kinds of light -- the glow that illuminates, and the glare that obscures. -James Thurber
Go to Top of Page

leoc50
Yak Posting Veteran

54 Posts

Posted - 2013-05-14 : 09:26:50
I would have to review my knowledge on 'left outer joins' , anyway I quickly tried the the query and seems it "may" work...Thank You!!!

I say "may" cuz trying another approach I realized the lookup table had some incomplete/bad records on its own; so for now I'm waiting on the person responsible for the information to fix the table.

Thanks again All for your help an ideas!
lec

- lec
Go to Top of Page
   

- Advertisement -