Solved

Excel query using SQL inner join command

Posted on 2010-08-17
6
499 Views
Last Modified: 2012-05-10
I am retrieving inventory information from a Pervasive db using excel query. I need the inventory info from 2 different tables. I want the results on 1 worksheet. The product number is the common field in both tables so I use INNER JOIN to get the price for the product (Price table) and match it to the stock name and quantities (inventory table). However the results give me duplicates (sort of). I get results with each product listed on 2 rows. One row has all of the stock information from the Inventory table as well as the pricing from the Price Tables (just what I want) however there is also a second row with just the results from the inventory table and 0 for price. How do i fix this?
SELECT partnum, description, price1, wholesale, ave_cost, on_hand FROM inventory, pricing
WHERE partnum = pricepartnum

Open in new window

0
Comment
Question by:RTsal
6 Comments
 
LVL 11

Accepted Solution

by:
Larissa T earned 500 total points
ID: 33458308
SELECT partnum, description, price1, wholesale, ave_cost, on_hand FROM inventory, pricing
WHERE partnum = pricepartnum and price1 <> 0
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33458648
SELECT partnum, description, price1, wholesale, ave_cost, on_hand
FROM inventory I INNER JOIN pricing P
ON I.partnum = P.pricepartnum
WHERE P.Price !=0
0
 
LVL 1

Expert Comment

by:geonon
ID: 33458754
try this:

SELECT inv.partnum, inv.description, pri.price1, pri.wholesale, pri.ave_cost, inv.on_hand
FROM inventory Inv, pricing pri
WHERE inv.partnum = pri.pricepartnum and price1 <> 0

I add alias and one more condition on where.

best

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33458831
0
 

Author Closing Comment

by:RTsal
ID: 33463452
This solves the problem but does not answer why the results returned 2 rows for every product number with one row containing a price of 0 and the other row containing the correct price
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33464719
It depends on usage and maintenance of pricing table
May be rows with 0 are used to list all products available in your system. Or it could be some errors in your system that inserted rows with price 0  or some leftover from debugging...

Run this
SELECT *
FROM pricing P WHERE P.Price =0
to see all those rows.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

15 Experts available now in Live!

Get 1:1 Help Now