| Author |
Topic |
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-03 : 09:55:33
|
I am having a problem with a SELECT query. I have had to add a relationship to one table, and I want to get the text from the 2nd table in relationship to the number in the main table.Old Query that reurned all records it was supposed to. In this query the field position_name did not have a null or blank value.SELECT fieldman_number , (last_name + ', ' + first_name) AS fullName , first_name , last_name , status , address1 , address2 , address3 , city , county , state , zip , email , home_phone , work_phone , extension , pager , cell_phone , car_phone , fax , affiliation , position_name , division_manager_flag , region_manager_flagFROM fm_fieldman_master WHERE (status = 'Active') OR (status = 'Inactive') ORDER BY fullName In the new query that includes an INNER JOIN, if there is a null or blank value in the position_number field, that row is not returned, but it should be.SELECT fm_fieldman_master.fieldman_number , (fm_fieldman_master.last_name + ' ,' + fm_fieldman_master.first_name) AS fullName , fm_fieldman_master.status , fm_fieldman_master.address1 , fm_fieldman_master.address2 , fm_fieldman_master.address3 , fm_fieldman_master.city , fm_fieldman_master.county , fm_fieldman_master.state , fm_fieldman_master.zip , fm_fieldman_master.email , fm_fieldman_master.home_phone , fm_fieldman_master.work_phone , fm_fieldman_master.extension , fm_fieldman_master.pager , fm_fieldman_master.cell_phone , fm_fieldman_master.car_phone , fm_fieldman_master.fax , fm_fieldman_master.affiliation , fm_fieldman_positions.position_name , fm_fieldman_master.division_manager_flag , fm_fieldman_master.region_manager_flagFROM fm_fieldman_positions INNER JOIN fm_fieldman_master ON fm_fieldman_positions.fieldman_position_number = fm_fieldman_master.position_numberWHERE (status = 'Active') OR (status = 'Inactive') ORDER BY fullName  What am I missing or doing wrong?Thanks for the helpCoachBarker |
|
|
whitefang
Enterprise-Level Plonker Who's Not Wrong
272 Posts |
Posted - 2009-05-03 : 10:07:32
|
| fm_fieldman_master ON ISNULL(fm_fieldman_positions.fieldman_position_number,0) = ISNULL(fm_fieldman_master.position_number,0)ORfm_fieldman_master ON (fm_fieldman_positions.fieldman_position_number = fm_fieldman_master.position_number) OR (fm_fieldman_positions.fieldman_position_number IS NULL AND fm_fieldman_master.position_number IS NULL) |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-03 : 10:18:18
|
| Nope returns the same results, 87 records, the query should be returning 99 records.Thanks for the helpCoachBarker |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2009-05-03 : 10:38:00
|
| If that column is null you will not get a result with INNER JOIN. You'll have to use a LEFT JOIN. |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-03 : 13:07:00
|
| So then as long as I have a value in there greater than zero then all rows will be returned. Easy enough to do.Thanks for the helpCoachBarker |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-04 : 09:05:06
|
| Thinking about that last post, I really do not want to have to always have a value in that filed, if no value is slected the default value will be 0, but I still want ALL records returned?Thanks for the helpCoachBarker |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-05-04 : 09:19:45
|
| use left join like suggested earlier. |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-04 : 11:37:19
|
| If you mean like so:FROM fm_fieldman_positions INNER JOIN fm_fieldman_master ON fm_fieldman_positions.fieldman_position_number = fm_fieldman_master.position_numberif a value is 0 that row is not returned.Thanks for the helpCoachBarker |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-05-04 : 11:42:55
|
| replace INNER JOIN with LEFT OUTER JOIN |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-04 : 12:14:00
|
| Pasted it back in wrong,FROM fm_fieldman_positions LEFT OUTER JOIN fm_fieldman_master ON fm_fieldman_positions.position_number = fm_fieldman_master.position_numberstill doesn't return a row with a 0 in the field.Thanks for the helpCoachBarker |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-05-04 : 12:48:54
|
| can you post the full query. |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-04 : 12:55:24
|
Sure canSELECT fm_fieldman_master.fieldman_number , (fm_fieldman_master.last_name + ' ,' + fm_fieldman_master.first_name) AS fullName , fm_fieldman_master.last_name , fm_fieldman_master.first_name , fm_fieldman_master.status , fm_fieldman_master.address1 , fm_fieldman_master.address2 , fm_fieldman_master.address3 , fm_fieldman_master.city , fm_fieldman_master.county , fm_fieldman_master.state , fm_fieldman_master.zip , fm_fieldman_master.email , fm_fieldman_master.home_phone , fm_fieldman_master.work_phone , fm_fieldman_master.extension , fm_fieldman_master.pager , fm_fieldman_master.cell_phone , fm_fieldman_master.car_phone , fm_fieldman_master.fax , fm_fieldman_master.affiliation , fm_fieldman_positions.position_name , fm_fieldman_master.division_manager_flag , fm_fieldman_master.region_manager_flag--FROM fm_fieldman_positions INNER JOIN-- fm_fieldman_master ON fm_fieldman_positions.position_number = fm_fieldman_master.position_numberFROM fm_fieldman_positions LEFT OUTER JOIN fm_fieldman_master ON fm_fieldman_positions.position_number = fm_fieldman_master.position_numberWHERE (status = 'Active') OR (status = 'Inactive') ORDER BY fullName Thanks for the helpCoachBarker |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-05-04 : 13:01:37
|
| in the above query, as long as the status is 'active' or 'inactive', that row will be returned. If the problem is the display, meaning its getting displayed as NULL, use ISNULL function to replace it with the value you want. |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-04 : 13:37:31
|
| Returned rows has nothing to do with active or inactive. It has to do with "fm_fieldman_positions.position_name" Table Positions# Name Status1 Area Supervisor A2 Membership A3 QC Specialist A4 Organics A5 Calibration A6 Manager AIf a person is not one of the above positions, he/she is then a Division or Regional Manager(1 person can be both) but I do not want the position 6 Manager A to have to be in the table, under position these people would have NULL for position but still be in the returned rows.If I have a null value in the position.name field, I still want that row returned with all the others.Thanks for the helpCoachBarker |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-05-04 : 13:53:53
|
| Returned rows have everything to do with Active or Inactive, that's what your WHERE clause is requiring. Why don't you want the postion 6 manager to be in the table? Do you wamt all the records from fm_fieldman_positions returned, or all the records from fm_fieldman_master returned. Please post sample data and expected output so we can help you better.Jim |
 |
|
|
CoachBarker
Posting Yak Master
170 Posts |
Posted - 2009-05-04 : 14:16:14
|
All records from fieldman_master, Active and Inactive are not the issue here, the issue is the field position_name. The Manager positon does not need to be in the table because there are 2 types of managers, divisional and regional, if you are a manager you can be one or both types. The managers field is taken care of using a checkbox. So if you are a manager then the field position_name would be NULL. quote: # FirstName Status Position_name Div_Man Reg_Man1 ALBERT Inactive 1 NULL NULL8 BRETT Active 0 NULL NULL9 BURKETT Active 4 NULL NULL10 CALVIN Active NULL NULL Y11 CARL Inactive 0 NULL NULL12 CHRIS Active 0 NULL NULL16 CINDY Outside 0 NULL NULL17 CLAYTON Active 3 NULL NULL18 CLYDE Active NULL NULL Y24 DAVE Outside 0 NULL NULL25 DAVE Active 0 NULL NULL26 DAVE Active NULL Y Y27 DAVID Active 0 NULL NULL28 DAVID Active 2 NULL NULL29 DAVID Active 0 NULL NULL30 DAVID Active NULL NULL Y
Table of Positions quote: # Name Status0 Area Supervisor A1 Membership A2 QC Specialist A3 Organics A4 Calibration A5 Manager A
the query would return all records but those that are "outside" for status.Thanks for the helpCoachBarker |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-04 : 14:24:45
|
I'm not 100% sure I'm following the logic, but maybe this gets you closer. If you can post data with table creation and insert statements that we can run queries against with the expected results, that would probably help:SELECT *, COALESCE(P.position_name, 'Regional Manager') AS position_nameFROM fm_fieldman_master AS MLEFT OUTER JOIN fm_fieldman_positions AS P ON M.fieldman_position_number = P.fieldman_position_number AND P.fieldman_position_number <> 5WHERE M.status IN ('Active', 'Inactive') ORDER BY M.fullName |
 |
|
|
|