Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Query Help

Posted on 2014-04-01
3
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

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 41

Accepted Solution

by:
Sharath earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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