Solved

Joining multiple tables

Posted on 2014-02-10
3
149 Views
Last Modified: 2014-02-11
SELECT     tblHistory.dtDate AS Date, tblHistory.dIDNumber AS [ID Number], tblStudent.achName AS [Patron Name], tblMealPlan.achMealPlanName AS [Meal Plan], 
                      tblDevices.achName AS Device, tblTransactionType.achTransactionType AS [Transaction Type], tblHistory.dAmount AS Amount, tblHistory.dBalance AS Balance, 
                      tblHistory.bPass AS Pass
FROM         tblHistory LEFT OUTER JOIN
                      tblDevices ON tblHistory.lDeviceNumber = tblDevices.lDeviceNumber LEFT OUTER JOIN
                      tblTransactionType ON tblHistory.lTransactionType = tblTransactionType.lTransactionType LEFT OUTER JOIN
                      tblMealPlan ON tblHistory.lMealPlan = tblMealPlan.lMealPlanNumber LEFT OUTER JOIN
                      tblStudent ON tblHistory.dIDNumber = tblStudent.dIDNumber
WHERE     (tblHistory.dtDate BETWEEN '1/14/14' AND '2/15/14') AND (NOT (tblStudent.achFax = 'TEST'))

Open in new window


How do i adjust this so that I will get every row from tbl.History.
0
Comment
Question by:Millkind
3 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39848611
remove your where clause considering dtDate
eg:
SELECT     tblHistory.dtDate AS Date, tblHistory.dIDNumber AS [ID Number], tblStudent.achName AS [Patron Name], tblMealPlan.achMealPlanName AS [Meal Plan], 
                      tblDevices.achName AS Device, tblTransactionType.achTransactionType AS [Transaction Type], tblHistory.dAmount AS Amount, tblHistory.dBalance AS Balance, 
                      tblHistory.bPass AS Pass
FROM         tblHistory LEFT OUTER JOIN
                      tblDevices ON tblHistory.lDeviceNumber = tblDevices.lDeviceNumber LEFT OUTER JOIN
                      tblTransactionType ON tblHistory.lTransactionType = tblTransactionType.lTransactionType LEFT OUTER JOIN
                      tblMealPlan ON tblHistory.lMealPlan = tblMealPlan.lMealPlanNumber LEFT OUTER JOIN
                      tblStudent ON tblHistory.dIDNumber = tblStudent.dIDNumber
WHERE     (NOT (tblStudent.achFax = 'TEST'))

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39848720
I suspect you want the date restrictions on tblHistory.  But you need to adjust the condition on tblStudent to allow for a NULL value, since values from that table might be missing:


WHERE     (tblHistory.dtDate BETWEEN 20140114' AND '20140215') AND (tblStudent.achFax IS NULL OR NOT (tblStudent.achFax = 'TEST'))
0
 
LVL 13

Accepted Solution

by:
magarity earned 500 total points
ID: 39848773
Better still to just move the student achFax requirement to the join section:

LEFT OUTER JOIN
                      tblStudent ON tblHistory.dIDNumber = tblStudent.dIDNumber
AND (NOT (tblStudent.achFax = 'TEST'))
WHERE     (tblHistory.dtDate BETWEEN '1/14/14' AND '2/15/14')
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

830 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