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 2012 Forums
 Transact-SQL (2012)
 SQL Join Help

Author  Topic 

Jim637
Starting Member

5 Posts

Posted - 2013-05-23 : 23:19:08
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

52326 Posts

Posted - 2013-05-24 : 00:39:46

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 - 2013-05-25 : 00:21:31
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

52326 Posts

Posted - 2013-05-25 : 09:28:38
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
   

- Advertisement -