Solved

Need Query to pull data from 2 tables with where clause

Posted on 2009-04-01
3
181 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
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

21 Experts available now in Live!

Get 1:1 Help Now