Solved

sql query

Posted on 2011-03-02
9
188 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
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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Encryption question 2 64
sql query help 2 62
What is this datetime? 1 25
Can I skip a node in XML? 9 29
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…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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