Solved

SQL: Inner Join Query assist

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 recover a database from a user managed backup
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 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

14 Experts available now in Live!

Get 1:1 Help Now