Solved

SQL: Inner Join Query assist

Posted on 2014-01-09
3
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 48

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 66

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

724 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