?
Solved

SQL Query Help

Posted on 2014-04-01
3
Medium Priority
?
277 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

771 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