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.
| Author |
Topic |
|
andrewcw
Posting Yak Master
133 Posts |
Posted - 2009-08-28 : 12:29:46
|
| I have 5 tables - I have tried to abstract my tables into a pattern that might be recognized , the select must work on the[ Orders ] -> [ ProductItems ] - key - [ Productdata] [ Orders ] -> [ Customers ] - key - [ Customerdata ]I can get one line of inner joins like this:FROM Orders INNER JOIN ProductItem ON Orders.Customer = SUBSTRING(ProductItems.CustomerJO, 1, 5) INNER JOIN ProductItems.PI_forKEY = ProductData.PI_KEYBUT I need to create an inner join to both tables - ProductItems & Customers then retreive also the specific product & customer data { another inner join }FROM Orders INNER JOIN ProductItems ON Orders.Customer = SUBSTRING(ProductItems.CustomerJO, 1, 5) and Also Orders INNER JOIN Customers ON Orders.Customer = SUBSTRING(Customers.CustomerJO, 1, 5)where Orders.Quantity > something.How can I create a query that will get all of this ? 2 tempTables that are inner joined on CustomerJO ? Thanks !andrewcw |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-08-28 : 14:23:31
|
something like this...SELECT a.field1,b.field2,c.field3FROM Orders a INNER JOIN ProductItems bON a.Customer = SUBSTRING(b.CustomerJO, 1, 5) INNER JOIN Customers cON a.Customer = SUBSTRING(c.CustomerJO, 1, 5)where a.Quantity > something |
 |
|
|
andrewcw
Posting Yak Master
133 Posts |
Posted - 2009-08-28 : 20:33:57
|
| I added INNER JOINS to the query for the additional data. Then the query rteurned 10000X times the rows I expected. I created small tables to see if I could see how to fix it. Now I realize its critical to specify both keys ....as shown...SELECT a.Customer, b.ProdName, c.something, e.cityFROM Orders AS a INNER JOIN ProductItems AS b ON a.Customer = SUBSTRING(b.CustomerJO, 1, 5) INNER JOIN Customers AS c ON a.Customer = SUBSTRING(c.CustomerJO, 1, 5) INNER JOIN ProductData AS d ON b.Prod_Fkey = d.Prod_key INNER JOIN CustomerData AS e ON c.Cust_FKEY = e.Cust_KEYWHERE (c.CustomerJO = 'JAMES_123') and b.Prod_Fkey =someNumber and c.Cust_FKEY=someotherNumberThanks Andrewandrewcw |
 |
|
|
|
|
|
|
|