Solved

SQL: Inner Join Query assist

Posted on 2014-01-09
3
574 Views
Last Modified: 2014-01-09
I have two tables:
Transactions
Products

Products contains an ProductID, Product name as well as other descriptors.  It can have multiple records for the same ID & Product Name.

Transactions contain a ProductID, category and various fields of information based on the sale of specific products.

I'm trying to query the Transactions and retrieve a count of transactions for a specific category along with the Product Name from the Products table.
The count is multiplying the number of records found in the Transaction table by the number of records in the Products table.

How can I correct this?

Query example:

Select Distinct count(a.pProductID)
,B.PRODUCT_NAME
from TRANSACTION_TABLE a
INNER JOIN PRODUCTS B
ON A.PRODUCT_ID = B.PRODUCT_ID
where substr(a.Category,1,6)='WDEFFD'
GROUP BY B.PRODUCT_NAME
0
Comment
Question by:GNOVAK
3 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39768113
"It can have multiple records for the same ID & Product Name."

You can't without creating some form of subquery that returns a single record for each Product ID.

, B.Product_Name
FROM Transaction_Table a
INNER JOIN (
SELECT DISTINCT Product_ID, Product_Name FROM Products) as B
ON a.Product_ID = B.Product_ID
WHERE substr(a.Category, 1, 6) = 'WDEFFD'
GROUP BY B.Product_Name
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39768132
>I'm trying to query the Transactions and retrieve a count of transactions for a specific category along with the Product Name from the Products table.

Agreed.  If a transaction.catetory can be related to multiple products, and needs to return a COUNT() of multiple products, then tell us what logic you want to use to choose amongst the many product names to display in the return set.
0
 

Author Closing Comment

by:GNOVAK
ID: 39768139
Helped me put my head on straight with very little caffeine  - Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Viewers will learn how the fundamental information of how to create a table.

809 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