Solved

sql query

Posted on 2011-03-02
9
191 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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