I have two tables I am trying to join. Table one contains financial data and table 2 contains vendor infomation that relates to the data via a vendor_no. Because of the data quality issues I have to deal with I cannot match up a vendor_no to a location (DETLOC) 100% of the time. In in a case where there is not match, would like to group those charges based on the region in which they occurred. This could be done via and org_code that links to a particular region, for instance, Atlanta.
Right now this is my code:
SELECT tbl_other_4.*, FY11_VENDOR.detloc
FROM tbl_other_4 LEFT JOIN FY11_vendor ON tbl_other_4.vendor_no=FY11_vendor.vendor_no
WHERE left(tbl_other_4.[org code],8)=left(FY11_vendor.organization,8)
the DETLOC is a unique identifier of a location to which a vendor provided a service. Because a vendor can provide services to more than one location, I also have to match it to the first 8 numbers in the organization code so that I get the correct DETLOC match.
Here is the ideal situation. Say I have ten charges that I know were made by vendors in Atlanta. However, I can only match 8 of those charges to specific DETLOC. In my current join, the other two charges would not show up b/c there is no match. I would like those two to be labeled "Atlanta" if there is no DETLOC match but the org_code matches Atlanta. Since I have 24 regions across the country, this would repeat itself for places like New York where I encountered the same problem with my data.
One of the things that puzzles me is that with the left join, I should get all of the rows from TBL_OTHER_4, regardless of weather there is a match in the FY11_vendor table. Then I could just look for the nulls and add in the values that way. However, I am only getting back rows where there is a match. Am I just doing something wrong with my join?
Thanks for the help,
Alex
Right now this is my code:
SELECT tbl_other_4.*, FY11_VENDOR.detloc
FROM tbl_other_4 LEFT JOIN FY11_vendor ON tbl_other_4.vendor_no=FY11_vendor.vendor_no
WHERE left(tbl_other_4.[org code],8)=left(FY11_vendor.organization,8)
the DETLOC is a unique identifier of a location to which a vendor provided a service. Because a vendor can provide services to more than one location, I also have to match it to the first 8 numbers in the organization code so that I get the correct DETLOC match.
Here is the ideal situation. Say I have ten charges that I know were made by vendors in Atlanta. However, I can only match 8 of those charges to specific DETLOC. In my current join, the other two charges would not show up b/c there is no match. I would like those two to be labeled "Atlanta" if there is no DETLOC match but the org_code matches Atlanta. Since I have 24 regions across the country, this would repeat itself for places like New York where I encountered the same problem with my data.
One of the things that puzzles me is that with the left join, I should get all of the rows from TBL_OTHER_4, regardless of weather there is a match in the FY11_vendor table. Then I could just look for the nulls and add in the values that way. However, I am only getting back rows where there is a match. Am I just doing something wrong with my join?
Thanks for the help,
Alex