Solved

Need Query to pull data from 2 tables with where clause

Posted on 2009-04-01
3
187 Views
Last Modified: 2013-12-12
I work for a company that does audit reports on the cleanliness of stores. I have 2 tables
HanStores and HanAR. HanStores contains data for every store in a chain, such as store location, name etc. HanAR contains all the data from each audit report. I need a query to list out every store (wether or not it has an audit report in the database), and if it does I need to list the data etc of the lastest report. So I need data from both tables. The following query worked fine:

"SELECT
T.`StoreNum` As StoreNum,
T.DistNum As DistNum,
T.Address1 As Address1,
T.City As City,
T.State As State,
T1.DateTimeEntered As DateTimeEntered,
T1.Baseline As Baseline
From HanStores T
Left Join HanAR T1 On T.`StoreNum` = T1.StoreID
order by StoreNum Asc;";

T1.Baseline is a field that has a 'Y' or 'N' that indicates whether it is a baseline report... when I try to run the same query with a where clause of "Where T1.Baseline !='Y'" it limits the output to only stores that have audit reports which are not baseline... but I do not get the data from HanStores unless there is an entery in HanAR for that store...
I'm not sure if I have to do a different join or a subquery to get what I need...
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
3 Comments
 
LVL 28

Accepted Solution

by:
TextReport earned 500 total points
ID: 24039893
With the left outer join the field will be null so you need to include a check for this.
Cheers, Andrew
SELECT T.StoreNum As StoreNum
     , T.DistNum As DistNum
     , T.Address1 As Address1
     , T.City As City
     , T.State As State
     , T1.DateTimeEntered As DateTimeEntered
     , T1.Baseline As Baseline
From HanStores T
   Left Join HanAR T1 On T.StoreNum = T1.StoreID
Where T1.Baseline !='Y' OR T1.Baseline IS NULL
ORDER BY StoreNum Asc

Open in new window

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24039899
Looks like a NULL issue to me. Try adding another condition to your where clause to test for NULL.
Something like

...
where T1.Baseline = 'N' or T1.Baseline is null
...

The reason being that for rows where T1 is not there, all T1 fields will be NULL.

Olly.
0
 

Author Closing Comment

by:dannyg280
ID: 31565329
Thanks! For some reason I thought just using a not equal would include NULL as well... Now I know... Thanks again!
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

623 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