I am trying to write an Access query that joins a single table twice, then run a count on one field from each.
SELECT Wines.wID,
Wines.num,
Wines.Brand,
Wines.wYear,
Wines.Variety,
Wines.Score,
g.fName,
g.lName ,
Count(DISTINCT Votes.vote1) AS Vote1,
Count(DISTINCT Votes_1.vote2) AS Vote2
FROM ((Wines
LEFT JOIN Guests AS g
ON Wines.wID = g.wineID)
LEFT JOIN Votes
ON Wines.num = Votes.vote1)
LEFT JOIN Votes AS Votes_1
ON Wines.num = Votes_1.vote2
WHERE (((Wines.pID)=2))
GROUP BY Wines.wID, Wines.num, Wines.Brand,
Wines.wYear, Wines.Variety, Wines.Score, g.fName, g.lName
ORDER BY Wines.Score DESC , Count(DISTINCT Votes.vote1) DESC;
However, it does not work. COUNT(DISTINCT) does not seem to work in Access. Leaving it out allows the query to run, but I do not get the correct results. Using it just once works fine. I can’t seem to write a subquery in the COUNT statement and I’m not really sure what else to do.
You can use DCount, if you are working within MS Access, a subquery, or it may be possible to set up a count query and join to that. I am not sure where the duplication is coming from, so the notes below illustrate a few ideas. It is not properly tested.