Solved

Excel query using SQL inner join command

Posted on 2010-08-17
6
509 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Viewers will learn how the fundamental information of how to create a table.
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…

803 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