Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Joining multiple tables

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
Millkind
Asked:
Millkind
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
Scott PletcherSenior DBACommented:
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
 
magarityCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now