Solved

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

Posted on 2013-01-10
2
213 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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