Hi Folks,
Sorry if I have the forum wrong, It's more a SQL question than a SQL Server question, but it is intended to run on MS SQL Server at least. :)
I'm trying to construct a query and am having trouble wrapping my head around this so I'm hoping that someone can help me get it straight.
Here's a stripped-down version of the problem. There are 3 tables involved. (Actually 4, but I'm going to bypass one of them.)
We have a table of people -- I'll call it CUSTOMER -- that has the usual sorts of fields, e.g., CUSTOMER_ID, LAST_NAME, FIRST_NAME, EMAIL_ADDRESS, etc. Then, for reasons that I don't pretend to understand, the guy who originally wrote the application decided to put two other descriptors into a separate table. (He called them "items".) So, we have a table ITEM_TYPE that contains the fields ITEM_TYPE_ID and NAME. This table has exactly two rows for the two item types. (For simplicity, I'm going to bypass this table in the current query.) There is then another table ITEM with the fields: ITEM_ID, ITEM_TYPE_ID and NAME. The ITEM table has about 20 rows which basically are divided between the two types and a bunch of descriptors of each type to be associated to the people in the CUSTOMER table. Then there is another table CUSTOMER_TO_ITE M to associate the customers with the descriptors in ITEM. This table has fields: CUSTOMER_TO_ITE M_ID (its primary key, which is not otherwise used), CUSTOMER_ID, ITEM_ID.
The application that manages the data requires one descriptor from each item type for each customer. Therefore, each customer_id in the CUSTOMER_TO_ITE M table appears in two rows, once with an item of type 1 and once with an item of type 2. (Which, of course is why I would have made something like ITEM1_ID and ITEM2_ID fields in the CUSTOMER table, but that's neither here nor there since I don't really have a choice in the matter.)
Now I need to create a report that contains, among other things, fields from the customer table itself and BOTH item descriptions (NAMEs) and here's where I'm floundering.
Here's one version of what I've tried:
[code=sql]
SELECT c.CUSTOMER_ID,
c.FIRST_NAME,
c.LAST_NAME,
c.EMAIL_ADDRESS ,
i1.ITEM_ID,
i1.ITEM_TYPE_ID ,
i1.NAME,
i2.ITEM_ID,
i2.ITEM_TYPE_ID ,
i2.NAME
FROM CUSTOMER c inner join CUSTOMER_TO_ITE M ci
on c.CUSTOMER_ID=c i.CUSTOMER_ID,
ITEM i1,
ITEM i2
WHERE ci.item_id=i1.i tem_id
AND ci.item_id=i2.i tem_id
AND i1.item_type_id = 1
AND i2.item_type_id = 2
[/code]
This version, as written, returns no data. If I were to remove line 16, for example, it would return thousands of rows, with a separate row for each customer with every type 2 item. If nothing else, this strongly suggests to me that my problem lies in how the joins are set up. I've tried several other ideas, trying to figure out how to inner join both i1 and i2 to ci, but I have not been able to think of one that is coherent. Everything seems to be nonsense.
Any suggestions on how to construct this query?
Thanks,
Paul
Sorry if I have the forum wrong, It's more a SQL question than a SQL Server question, but it is intended to run on MS SQL Server at least. :)
I'm trying to construct a query and am having trouble wrapping my head around this so I'm hoping that someone can help me get it straight.
Here's a stripped-down version of the problem. There are 3 tables involved. (Actually 4, but I'm going to bypass one of them.)
We have a table of people -- I'll call it CUSTOMER -- that has the usual sorts of fields, e.g., CUSTOMER_ID, LAST_NAME, FIRST_NAME, EMAIL_ADDRESS, etc. Then, for reasons that I don't pretend to understand, the guy who originally wrote the application decided to put two other descriptors into a separate table. (He called them "items".) So, we have a table ITEM_TYPE that contains the fields ITEM_TYPE_ID and NAME. This table has exactly two rows for the two item types. (For simplicity, I'm going to bypass this table in the current query.) There is then another table ITEM with the fields: ITEM_ID, ITEM_TYPE_ID and NAME. The ITEM table has about 20 rows which basically are divided between the two types and a bunch of descriptors of each type to be associated to the people in the CUSTOMER table. Then there is another table CUSTOMER_TO_ITE M to associate the customers with the descriptors in ITEM. This table has fields: CUSTOMER_TO_ITE M_ID (its primary key, which is not otherwise used), CUSTOMER_ID, ITEM_ID.
The application that manages the data requires one descriptor from each item type for each customer. Therefore, each customer_id in the CUSTOMER_TO_ITE M table appears in two rows, once with an item of type 1 and once with an item of type 2. (Which, of course is why I would have made something like ITEM1_ID and ITEM2_ID fields in the CUSTOMER table, but that's neither here nor there since I don't really have a choice in the matter.)
Now I need to create a report that contains, among other things, fields from the customer table itself and BOTH item descriptions (NAMEs) and here's where I'm floundering.
Here's one version of what I've tried:
[code=sql]
SELECT c.CUSTOMER_ID,
c.FIRST_NAME,
c.LAST_NAME,
c.EMAIL_ADDRESS ,
i1.ITEM_ID,
i1.ITEM_TYPE_ID ,
i1.NAME,
i2.ITEM_ID,
i2.ITEM_TYPE_ID ,
i2.NAME
FROM CUSTOMER c inner join CUSTOMER_TO_ITE M ci
on c.CUSTOMER_ID=c i.CUSTOMER_ID,
ITEM i1,
ITEM i2
WHERE ci.item_id=i1.i tem_id
AND ci.item_id=i2.i tem_id
AND i1.item_type_id = 1
AND i2.item_type_id = 2
[/code]
This version, as written, returns no data. If I were to remove line 16, for example, it would return thousands of rows, with a separate row for each customer with every type 2 item. If nothing else, this strongly suggests to me that my problem lies in how the joins are set up. I've tried several other ideas, trying to figure out how to inner join both i1 and i2 to ci, but I have not been able to think of one that is coherent. Everything seems to be nonsense.
Any suggestions on how to construct this query?
Thanks,
Paul
Comment