Solved

Need Query to pull data from 2 tables with where clause

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can't find PHP files on account that has WordPress 3 24
PHP AJAX Wordpress 9 19
Sql server function help 15 26
SQL Server - Set Value of Multiple Fields in One Query 10 22
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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 …

815 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

11 Experts available now in Live!

Get 1:1 Help Now