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 2012 Forums
 Transact-SQL (2012)
 SQL Join Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Jim637
Starting Member

5 Posts

Posted - 05/23/2013 :  23:19:08  Show Profile  Reply with Quote
Hi guys i have question.

Here what i want. I tried to create a sample data.

in my sql syntax below I want to see first from table (problem) if i get the value then don't Ago for next table (charge) if i didn't find match from table (problem) then check table(charge)


Here is my query

Select
ID,
FNAME,
LNAME
FROM TABLE1
HERE I WANT TO USE MY CASE STATEMENT
CASE
WHEN TABLE (PROBLEM) LINK WITH TABLE1 (GET THE VALUE)
ELSE
WHEN TABLE (CHARGE) LINK WITH TABLE1 (GET THE VALUE)

Thank You.

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 05/24/2013 :  00:39:46  Show Profile  Reply with Quote

stub will look like below

Select
ID,
FNAME,
LNAME,
COALESCE(p.col,c.col),
...
FROM TABLE1 t1
LEFT JOIN PROBLEM p
ON condition
LEFT JOIN CHARGE c
ON condition


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Jim637
Starting Member

5 Posts

Posted - 05/25/2013 :  00:21:31  Show Profile  Reply with Quote
Hi Visakh thank you for your reply. Quick question.
Select
ID,
FNAME,
LNAME,
COALESCE(p.col,c.col), (If I skip this COALESCE,Do you think it should be fine?, I am using Union in 5 queries. So i don't need this)
...
FROM TABLE1 t1
LEFT JOIN PROBLEM p
ON condition
LEFT JOIN CHARGE c
ON condition
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 05/25/2013 :  09:28:38  Show Profile  Reply with Quote
the coalesce is only required if you want value to be retrieved from any of the left join tables, otherwise you can dispense with it

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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