Solved

SQL Query Help

Posted on 2014-04-01
3
276 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 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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 video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

687 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