COUNT () 2 columnas con 2 uniones separadas en Access?

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;

Sin embargo, no funciona. 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.

preguntado el 04 de julio de 12 a las 02:07

1 Respuestas

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.

SELECT Wines.wID, 
   Wines.num, 
   Wines.Brand, 
   Wines.wYear, 
   Wines.Variety, 
   Wines.Score, 
   g.fName, 
   g.lName , 
   DCount("vote1","votes","vote1"=Wines.num) AS Vote1,  
   (SELECT Count(Vote2) FROM 
      (SELECT DISTINCT vote2 
       FROM votes v
       INNER JOIN Wines w ON v.vote2=w.Num
       WHERE w.pID=2 AND w.num=Wines.num)) AS Vote2
FROM   Wines 
           LEFT JOIN Guests AS g 
              ON Wines.wID = g.wineID

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 , vote1 DESC;

Respondido 04 Jul 12, 11:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.