?
Solved

sql query

Posted on 2011-03-02
9
Medium Priority
?
195 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
Independent Software Vendors: 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 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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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