Solved

SQL Query Help

Posted on 2014-04-01
3
272 Views
Last Modified: 2014-04-01
I have a really massive SQL query that I use to run an export from one CRM into another CRM package.  I just uncovered a problem with my query and I am not sure how to fix it.  My basic syntax is this:

With C1 As
(
Select statement
),
C2 As
(
Select statement
)
select c1.value, C2.value
from C1
left join c2

I have 17 different joins here because this query is huge. Here is my question.  One of the select queries in the top part of the statement is returning a null value sometimes.  When it returns a null I need to run a different query to produce the correct values.  I always have trouble creating programming logic in SQL but I basically want:

if query = NULL then query2.

Can anyone make any sense of this and tell me how I can accomplish this?  I am running in MS SQL 2008.

Thank you!
0
Comment
Question by:mattpayne59
3 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39970254
You need to use ISNULL and CASE in your statements.
0
 

Author Comment

by:mattpayne59
ID: 39970264
Ok but how do I structure it because I dont know if I need the other case until after I complete the select query?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39970368
lochan is correct. In order to plugin the ISNULL/COALESCE or CASE, you have to provide the whole query.
Or try embedding like this.
With C1 As
(
Select statement
),
C2 As
(
Select statement
)
select COALESCE(c1.value,C2.value) as Value
from C1
left join c2

Open in new window

0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question