Solved

sql query

Posted on 2011-03-02
9
184 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now