Solved

Need help pulling data will NULL fields from MS SQL query.

Posted on 2013-01-10
2
215 Views
Last Modified: 2013-03-11
Hello,
    I have a query which pulls contact data from accounts that a salesman has and also includes sales and profit data from the prior year. This works fine, except if the account had no activity in 2012 (or no entry for 2012 in the arcsales table) then it doesn't pull any data for that account. I need it to pull the contact info regardless if there is Sales and Profit info. I've been told I need to check for null fields for T3.YTDTotSales and
  T3.YTDPubProfit but I am ubsure how to do this.

SELECT
  T.CustAcct,
  T.CustomerName,
T.Address1,
T.Address2,
  T.City,
  T.State,
  T.Zip,T3.YTDTotSales,
  T3.YTDPubProfit
  T1.FirstName As SalesContactFirstName,
  T1.LastName As SalesContactLastName,
T1.EmailAddress,
  T2.SmanCode As SmanCode,

FROM arcusts T
  Left Outer Join arcconts T1 On T.SalesContactID = T1.CustContactID
  Left Outer Join arsmen T2 On T.SmanID = T2.SmanID
  Left Outer Join arcsales T3 On T.CustID = T3.CustID
WHERE T3.FiscalYear=2012
and T2.SmanCode='Salesman1'

Open in new window

0
Comment
Question by:dannyg280
[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
  • 2
2 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 38764778
putting the conditions in the where clause effectively turns your outer joins into inner joins

try keeping them in the join clause


SELECT
  T.CustAcct,
  T.CustomerName,
T.Address1,
T.Address2,
  T.City,
  T.State,
  T.Zip,T3.YTDTotSales,
  T3.YTDPubProfit
  T1.FirstName As SalesContactFirstName,
  T1.LastName As SalesContactLastName,
T1.EmailAddress,
  T2.SmanCode As SmanCode,

FROM arcusts T
  Left Outer Join arcconts T1 On T.SalesContactID = T1.CustContactID
  Left Outer Join arsmen T2 On T.SmanID = T2.SmanID
  Left Outer Join arcsales T3 On T.CustID = T3.CustID
                           and T3.FiscalYear=2012
                           and T2.SmanCode='Salesman1'
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 38764784
what do you mean by "check for null values"

if you find them, what do you want to do?
you can use  

     and some_column is null    

in the where clause or join conditions if you want to look for nulls
or something like

     isnull(t.custacct,' null account')  as custacct,  

to replace nulls with a value in the select
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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