Hello everyone and thank you in advance for your help.
I’m having troubles with an SQL query in access.
I have Database
I need the following output ( show and list the Store Number and PaidMoney ONLY where Paid money is the same amount 2 or more times only) desired output
I already have 2 queries which kind of solve the problem, but each of the queries I have only solve 50% of the answer I need, the first lists all the results not only the duplicates , and the second query tells me how many duplicates there are but doesnt list and show the duplicates to me.
First Query
SELECT StoreNumber, PaidMoney FROM Stores
Second query
SELECT StoreNumber, PaidMoney, COUNT(*) FROM Stores GROUP BY StoreNumber, PaidMoney HAVING COUNT(*) > 1
Thank you all for your time and help!
Advertisement
Answer
You can join the 2 queries together and only “select” the columns from the first query. The second query will just filter out the rows from the first query that only appear once. Rows from the first query that appear more than once should appear as many times as they appear in the first query.
SELECT q1.StoreNumber, q1.PaidMoney FROM (SELECT StoreNumber, PaidMoney FROM Stores) q1 INNER JOIN (SELECT StoreNumber, PaidMoney, COUNT(*) ct2 FROM Stores GROUP BY StoreNumber, PaidMoney HAVING COUNT(*) > 1) q2 ON q1.StoreNumber = q2.StoreNumber AND q1.PaidMoney = q2.PaidMoney