?
Solved

sql query

Posted on 2011-03-02
9
Medium Priority
?
192 Views
Last Modified: 2012-05-11
hello,
i am trying use a join to get all the data from both the tables, I am struggling to get dedires result

product
------------------
productid      productnumber   stock
563            1334            13
564            1338            8

product_attributes
-------------------
poselid             poselstock      poselsku
p563:o26:12m        5            1335      
p563:o26:18m        6            1336      
p563:o26:6m              2            1334      

I want to match the productnumber with poselsku and all the 3 records from the product_attributes table and show the stock from poselstock

The desired output would be
productid      productnumber      stock
-----------------------------------
563             1334             2
563             1335             5
563             1336             6
564             1338             8

I hope you see what I am trying to do?

Thanks

0
Comment
Question by:newbie27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35015740
You can use union:

Select * from product union select * from product_attributes
0
 
LVL 6

Expert Comment

by:kswathi
ID: 35015751
Try the below code:

Select P.ProductId,
            P.productnumber,
            PA.Stock
from product P
left
outer join product_attributes PA
on    P.productnumber = PA.poselsku

0
 
LVL 8

Author Comment

by:newbie27
ID: 35015766
I am getting this exception ...

>>All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Also, if you could please note, for the attributes stock, I am not showing from the produts table, instead i want to show the stock available in the products_attributes table for the match

thanks
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 8

Author Comment

by:newbie27
ID: 35015774
>>kswathi:

thanks

I am only getting one match from the attribtues table

563             1334             2
564             1338             8
0
 
LVL 6

Expert Comment

by:kswathi
ID: 35015798
try right outer join
0
 
LVL 6

Expert Comment

by:kswathi
ID: 35015826
try the links

or  CROSS JOIN
http://en.wikipedia.org/wiki/Join_(SQL)
0
 
LVL 8

Author Comment

by:newbie27
ID: 35015890
I am not getting it right, please advise

this is what I am trying to do

if we find the product with this match product.productnumber = product_attributes.productnumber

then fetch product.productid, and get all the rows from the product_attributes matching th poselid          

product.productid = : 563    
product_attributes = left(p563:o26:6m,4)

so basically for 563, there are 3 rows in the attributes table which I should get in the result ...

I hope someone may understand what I am trying to say?

 
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 2000 total points
ID: 35016164
SELECT p.productid, p.productnumber, COALESCE(a.poselstock,p.stock) stock
FROM product p LEFT JOIN
     product_attributes a ON CAST(p.productid as varchar)=SUBSTRING(a.poselid,2,3)
ORDER BY p.productid, a.poselsku

Open in new window

0
 
LVL 8

Author Closing Comment

by:newbie27
ID: 35016559
Thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question