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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help in a create a view with CASE with a select...
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

leoc50
Yak Posting Veteran

USA
54 Posts

Posted - 05/09/2013 :  14:24:49  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3744 Posts

Posted - 05/09/2013 :  15:07:41  Show Profile  Reply with Quote
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

USA
54 Posts

Posted - 05/09/2013 :  15:16:22  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1770 Posts

Posted - 05/10/2013 :  14:35:25  Show Profile  Reply with Quote
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]
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

USA
54 Posts

Posted - 05/14/2013 :  09:26:50  Show Profile  Reply with Quote
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
  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.08 seconds. Powered By: Snitz Forums 2000