Solved

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

Posted on 2013-01-10
2
206 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
  • 2
2 Comments
 
LVL 73

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 73

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.…
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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now