473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Query problem

22
I have 2 tables in my query. M_Employees and M_Notes

M_Employees
empSSN - number, pk
empLastName - text
etc.

M_Notes
notesID - autonumber, pk
notesSSN - number, linked to SSN in M_Employees table
notesNote - text
etc.

I put both of these tables in a query because I need to make a report with data from both.

My problem: My query will only return results if there is a note in the M_Notes table. I've tried changing it to a LEFT join, RIGHT join, and back to INNER join but I still don't get any employee data (even if they meet the criteria I set) unless there is an associated note in the notes table.

Here is the SQL for my query:

Expand|Select|Wrap|Line Numbers
  1. SELECT M_Employees.empExamMonth, M_Employees.empType, M_Employees.empAgency, M_Employees.empOrg, L_Org.OrgPOC, M_Employees.empSSN, M_Employees.empNameManpower, M_Employees.empPayPlan, M_Employees.empSeries, M_Employees.empPosition, M_Employees.empTOD, M_Employees.empHours, M_Employees.empSupervisor, M_Notes.NoteType, M_Notes.NoteDetail, M_Notes.NoteStatus
  2. FROM (L_Org INNER JOIN M_Employees ON L_Org.OrgID = M_Employees.empOrg) INNER JOIN M_Notes ON M_Employees.empSSN = M_Notes.NoteSSN
  3. WHERE (((M_Employees.empExamMonth) Like [Enter Month]) AND ((M_Employees.empType)=6) AND ((M_Employees.empAgency)=1) AND ((M_Notes.NoteType)=2) AND ((M_Notes.NoteStatus)=Yes)) OR (((M_Employees.empType)=7) AND ((M_Employees.empAgency)=2));
Any idea how to fix this so I get the employee data even if they don't have any notes in the M_Notes table?

Thanks,
Bekah
Nov 25 '09 #1

✓ answered by Frinavale

(Wiki for:SQL Join)

When using an inner join, if there is no matching data to join the records on then these records (that have no match) will not be included in the resulting table.

An outer join does not require each record in the two joined tables to have a matching record....but you said that this doesn't work?

Did you try LEFT OUTTER JOIN?

(see wiki link because I could be wrong about LEFT)

-Frinny

8 3116
Frinavale
9,735 Expert Mod 8TB
I could be wrong but I think this problem has to do with your Where statement you have:
Expand|Select|Wrap|Line Numbers
  1. AND ((M_Notes.NoteType)=2) AND ((M_Notes.NoteStatus)=Yes
  2.  
If the NoteStatus is not Yes or NoteType is not 2, then the record won't meet the criteria.

-Frinny
Nov 25 '09 #2
rleepac
22
Frinny, I could see how you would come to that conclusion but I don't think that's it.

NoteStatus just indicates if it is a current note or not. So NoteStatus = Yes just pulls the current notes.

NoteType just indicates what category the note falls in. So NoteType = 2 means that I only want notes that fall into category 2.

I thought those criteria might be the problem too so I tried running the query without those criteria and I still only got employee records that had a note (of any type or status) attached.

Maybe I need to put criteria in the M_Notes.NoteID like = Null or NotNull?
I don't know the right way to put that in there but I'll play with that option next.
Nov 25 '09 #3
Frinavale
9,735 Expert Mod 8TB
(Wiki for:SQL Join)

When using an inner join, if there is no matching data to join the records on then these records (that have no match) will not be included in the resulting table.

An outer join does not require each record in the two joined tables to have a matching record....but you said that this doesn't work?

Did you try LEFT OUTTER JOIN?

(see wiki link because I could be wrong about LEFT)

-Frinny
Nov 25 '09 #4
rleepac
22
Got it! I was entering the criteria wrong. I had it as an AND instead of an OR.

This is the code that got me all the employees that met the criteria regardless of if they had any notes attached. But if they did have any notes they would show up if they met certain criteria.

Whew....thanks for your help!

Expand|Select|Wrap|Line Numbers
  1. SELECT M_Employees.empExamMonth, M_Employees.empType, M_Employees.empAgency, M_Notes.NoteID, M_Employees.empOrg, L_Org.OrgPOC, M_Employees.empSSN, M_Employees.empNameManpower, M_Employees.empPayPlan, M_Employees.empSeries, M_Employees.empPosition, M_Employees.empTOD, M_Employees.empHours, M_Employees.empSupervisor, M_Notes.NoteType, M_Notes.NoteDetail, M_Notes.NoteStatus, M_Notes.NoteDetail
  2. FROM (L_Org INNER JOIN M_Employees ON L_Org.OrgID = M_Employees.empOrg) LEFT JOIN M_Notes ON M_Employees.empSSN = M_Notes.NoteSSN
  3. WHERE (((M_Employees.empExamMonth) Like [Enter Month]) AND ((M_Employees.empType)=6 Or (M_Employees.empType)=9) AND ((M_Employees.empAgency)=1 Or (M_Employees.empAgency)=2) AND ((M_Notes.NoteType)=2 Or (M_Notes.NoteType) Is Null) AND ((M_Notes.NoteStatus)=Yes Or (M_Notes.NoteStatus) Is Null));
Nov 25 '09 #5
topher23
234 Expert 100+
I was looking over your tables and realized that you might be walking a fine line here. Your tables are using the employee's SSN as an identifier, which is a security no-no, particularly in the state of California, which has the strictest SSN privacy laws in the nation.

The best practice for employee records is to use an AutoNumber field to generate a unique value for each employee, then use that value elsewhere in the database to link to the employee. If the SSN must be in the database, it should be stored once and not referenced by any other table. My preference is actually to store it in a separate table linked to the original employee record with the AutoNumber field, just to maintain a certain level of separation.

Also, any database containing social security numbers needs to be on a secure system, development copies need to be isolated from the network at large (do development on a firewalled computer and don't store development copies on the server), and production databases should be compiled to .mde or .accde.

SSN's are not something to take lightly in this era of identity theft, and if someone gets even a single SSN from your database, your company could be facing major fines and you may even lose your job.
Nov 25 '09 #6
rleepac
22
Yes, I'm well aware of the problems with using the SSN. I don't have a choice. I'm a contractor providing medical services and the agency I work for files their medical records by SSN. It would literally take a congressional action to change that. However, I do like your idea of storing them in a separate table. I'll have to look at how that will affect the way we access our data. Definitely worth taking a look at. Thanks.

That said, I am on a secured server with development copies isolated, and the production database will be compiled to accde.

Thanks for the info...definitely don't want to end up in jail or unemployed!
Nov 25 '09 #7
topher23
234 Expert 100+
I totally understand. I worked as a Patient Administrator in the Army for 8 years - we lived and died by the SSN.

When I had to build a deployable medical tracking system for my regiment, I spent a lot of time and several major rewrites of my system making the SSN accessible to those who needed it, yet at the same time hard to access for someone without a need to know. After a lot of study, I ended up with what I outlined previously.

The nice part is, you can still have forms and reports display all or part of the SSN by using queries, but by not using it directly as an identifier in the tables you restrict access by making it only available to people with permissions to view those forms and reports.
Nov 25 '09 #8
NeoPa
32,556 Expert Mod 16PB
It seems this was a JOIN problem AND a WHERE criteria problem.

Frinny got both so all good now.
Nov 29 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
1
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
4
by: Konrad Hammerer | last post by:
Hi! I have the following problem: I have a query (a) using another query (b) to get the amount of records of this other query (b), means: select count(MNR) as Number from...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.