Hi all
I have an issue creating a query, which is arkward.
Basically, I have a table containing items for sale which is linked to a seperate table containing codes for the items. But some clients require their own descriptions and these descriptions could relate to more than one of my codes. Therefore, I have the following tables:
Client Table
ClientID (primary)
ClientName
Mapping_Client Table
Mapping_ClientID (primary)
ClientID
ClientDesc
Mapping_Code Table
Mapping_Codes_ID (primary)
Mapping_Client_ID
Code_ID
Codes Table
CodeID (primary)
CodeDesc
Item Table
ItemID (primary)
CodeID
Desc
What I want is a query to pull back all the items and their corresponding client descriptions from the Mapping_Client table. However, if there is no client description, then I want to use the description from the Code table. The SQL can just have NULLs in either of these two fields as I can check in my code for this condition.
I started the query using basic left joins, but realised it was pulling back duplicate rows (which cannot happen). I could have used an inner join, but this relied upon a client desc being present.
My query is as follows:
Basically, the query above wil pull back duplicates because the 2nd join is pulling back other client info and the client join is the 3rd join. If I used inner join's then it works, but does not cater for clients with no client desc's and in this case I need to use the codes desc.
Help in the right direction would be appreciated.
Thanks
I have an issue creating a query, which is arkward.
Basically, I have a table containing items for sale which is linked to a seperate table containing codes for the items. But some clients require their own descriptions and these descriptions could relate to more than one of my codes. Therefore, I have the following tables:
Client Table
ClientID (primary)
ClientName
Mapping_Client Table
Mapping_ClientID (primary)
ClientID
ClientDesc
Mapping_Code Table
Mapping_Codes_ID (primary)
Mapping_Client_ID
Code_ID
Codes Table
CodeID (primary)
CodeDesc
Item Table
ItemID (primary)
CodeID
Desc
What I want is a query to pull back all the items and their corresponding client descriptions from the Mapping_Client table. However, if there is no client description, then I want to use the description from the Code table. The SQL can just have NULLs in either of these two fields as I can check in my code for this condition.
I started the query using basic left joins, but realised it was pulling back duplicate rows (which cannot happen). I could have used an inner join, but this relied upon a client desc being present.
My query is as follows:
Code:
select item.itemid, mapping_client.clientdesc, codes.desc
from item
left join codes on codes.codeid=item.codeid
left join mapping_code on mapping_code.codeid=codes.codeid
left join mapping_client on mapping_client.mapping_clientid=mapping_code.mapping_clientid and mapping_client.clientid=10
Basically, the query above wil pull back duplicates because the 2nd join is pulling back other client info and the client join is the 3rd join. If I used inner join's then it works, but does not cater for clients with no client desc's and in this case I need to use the codes desc.
Help in the right direction would be appreciated.
Thanks