Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query join problem

Author  Topic 

MyronCope
Starting Member

46 Posts

Posted - 2011-04-07 : 08:43:46
using sql server 2005

I have the following tables:

ORDERS: main table that stores one record for each order, PK is orderNum

ITEMS: contains all the items that are shipped with each order, can reference each record with orderNum

SPEC_ITEMS: contains special items that are also contained in each order, can reference each record with orderNum

What I need to do is the following: I need one query that will return a record for each ITEM belonging to the order and also each SPEC_ITEM that belongs to each order. In the first query below I am getting each ITEM record and then in the second query I joined to the SPEC_ITEM table but I need to figure out how to also return each SPEC_ITEM record that belongs to each record.

I have the following query that will return all items from the "ITEMS" table:


<SQL>

SELECT ITEMS.ItemDesc
As ItmDesc
FROM dbo.ORDERS

left outer join ITEMS on
ORDERS.orderNum = ITEMS.orderNum

WHERE ORDERS.ordernum='1223'

--query below joins on SPEC_ITEMS

SELECT ITEMS.ItemDesc

FROM dbo.ORDERS

left outer join ITEMS on
ORDERS.orderNum = ITEMS.orderNum

left outer join SPEC_ITEMS on
ORDERS.orderNum = SPEC_ITEMS.orderNum

WHERE ORDERS.ordernum='1223'
</SQL>



Please advise on what I need to do, I think that my join for the SPEC_ITEMS table might not be right.



so this returns all Items for this order from the ITEMS table ok, for this order there are 5 items.
However I also need to return the special items that are in the SPEC_ITEMS table.

I need 2 things:
1. The ItmDesc field should pull from either ITEMS if the record is a regular item and from SPEC_ITEMS if it is a special item but more importantly
2. I need to display also a record for each SPEC_ITEM that matches the order.

I added the join to the SPEC_ITEMS table too but this only brings back the 5 items from the ITEMS table, there is an additional special item from the SPEC_ITEMS
table that should also display

thanks

MC

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-04-07 : 08:59:33
Could you provide sample data for each table and what you want the output to look like?

Thanks,

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

MyronCope
Starting Member

46 Posts

Posted - 2011-04-07 : 09:07:27
quote:
Originally posted by jimf

Could you provide sample data for each table and what you want the output to look like?

Thanks,

Jim

Everyday I learn something that somebody else already knew


Sure, Sample data is like for ItemDesc field that is something like this:

ITEMS.ItemDesc: "Standard Drain", OR "Plastic Trench"

SPEC_ITEMS: "Customized Drain" OR "Assembled Item"

The most important thing is that I want to return a record for the SPEC_ITEMS rows that match the orderNum, I can play around with the other fields that I"m going to add after that.

AT the moment it returns the ITEMS records which is a good first step but I also need to return the SPEC_ITEMS records too. Also I need itmDesc to be filled with ITEMS.itemDesc for normal item or itmDesc will pull from SPEC_ITEMS.ItemDesc for special items.

after that I'll add in fields like "Item_Code" that exists in both the ITEMS table and SPEC_ITEMS table but thats somethign I can figure out, I need to get the record count good first (meaning return all records from both ITEMS and SPEC_ITEMS for the given order).

sorry if I'm repeating, just trying to be clear.
thanks
MC
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-04-07 : 09:27:29
Sample data more like at the link. Enough info so we can better help you.

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

MyronCope
Starting Member

46 Posts

Posted - 2011-04-07 : 11:39:44
thanks, I'll try to follow that format next time.

I realized that I just needed a simple join between ITEMS and the SPEC_ITEMS table, that did the trick.

Is it friday yet?

MC
Go to Top of Page
   

- Advertisement -