Solved

SQL: Inner Join Query assist

Posted on 2014-01-09
3
576 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to recover a database from a user managed backup
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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