MS ACCESS: ¿Cómo puedo contar un valor distinto usando la consulta de acceso?
Pregunta
aquí está la consulta compleja actual que se muestra a continuación.
SELECT DISTINCT Evaluation.ETCode, Training.TTitle, Training.Tcomponent, Training.TImpliment_Partner, Training.TVenue, Training.TStartDate, Training.TEndDate, Evaluation.EDate, Answer.QCode, Answer.Answer, Count(Answer.Answer) AS [Count], Questions.SL, Questions.Question
FROM ((Evaluation INNER JOIN Training ON Evaluation.ETCode=Training.TCode) INNER JOIN Answer ON Evaluation.ECode=Answer.ECode) INNER JOIN Questions ON Answer.QCode=Questions.QCode
GROUP BY Evaluation.ETCode, Answer.QCode, Training.TTitle, Training.Tcomponent, Training.TImpliment_Partner, Training.Tvenue, Answer.Answer, Questions.Question, Training.TStartDate, Training.TEndDate, Evaluation.EDate, Questions.SL
ORDER BY Answer.QCode, Answer.Answer;
Hay otra columna Training.TCode. Necesito contar con un código de entrenamiento diferente, ¿alguien puede ayudarme? Si necesita más información, hágamelo saber
Solución
prueba
select ..., count(distinct Training.Tcode) as ..., ...
EDITAR: ahora mira esto ...
Toma el siguiente código SQL. La primera selección es cómo el servidor SQL haría esto y la segunda consulta debería ser compatible con el acceso ...
declare @t table (eCode int, tcode int)
insert into @t values(1,1)
insert into @t values(1,1)
insert into @t values(1,2)
insert into @t values(1,3)
insert into @t values(2,2)
insert into @t values(2,3)
insert into @t values(3,1)
select
ecode, count(distinct tCode) countof
from
@t
group by
ecode
select ecode, count(*)
from
(select distinct tcode, ecode
from @t group by tcode, ecode) t
group by ecode
Devuelve lo siguiente:
ecode tcode
1 3 (there are 3 distinct tcode for ecode of 1)
2 2 (there are 2 distinct tcode for ecode of 2)
3 1 (there is 1 distinct tcode for ecode of 3)
Otros consejos
Publiqué una pregunta similar hace aproximadamente un año en los grupos de Google. Recibí una excelente respuesta:
Una tabla cruzada puede hacer (a partir de una propuesta original de Steve Dassin) siempre a medida que cuenta el fondo, el subfondo:
TRANSFORM COUNT(*) AS theCell
SELECT ValDate,
COUNT(*) AS StandardCount,
COUNT(theCell) AS DistinctCount
FROM tableName
GROUP BY ValDate
PIVOT fund IN(Null)
que, para cada día (grupo), devolverá el número de registros y el número de fondos diferentes (distintos).
Cambiar
PIVOT fund IN(Null)
a
PIVOT subfund IN(Null)
para obtener lo mismo, para los subfondos.
Esperando que pueda ayudar, Vanderghast, MVP de acceso
No sé si esto funcionará, pero aquí hay un enlace a esa publicación .
Sadat, usa una subconsulta como esta:
SELECT DISTINCT Evaluation.ETCode, Training.TTitle, Training.Tcomponent, Training.TImpliment_Partner, Training.TVenue, Training.TStartDate, Training.TEndDate, Evaluation.EDate, Answer.QCode, Answer.Answer, Count(Answer.Answer) AS [Count], Questions.SL, Questions.Question,
(SELECT COUNT(*) FROM Training t2 WHERE t2.TCode = Evalution.ETCode) as TCodeCount
FROM ((Evaluation INNER JOIN Training ON Evaluation.ETCode=Training.TCode) INNER JOIN Answer ON Evaluation.ECode=Answer.ECode) INNER JOIN Questions ON Answer.QCode=Questions.QCode
GROUP BY Evaluation.ETCode, Answer.QCode, Training.TTitle, Training.Tcomponent, Training.TImpliment_Partner, Training.Tvenue, Answer.Answer, Questions.Question, Training.TStartDate, Training.TEndDate, Evaluation.EDate, Questions.SL
ORDER BY Answer.QCode, Answer.Answer;
Logré hacer un recuento de valor distinto en Access haciendo lo siguiente:
select Job,sum(pp) as number_distinct_fruits
from
(select Job, Fruit, 1 as pp
from Jobtable group by Job, Fruit) t
group by Job
Debe tener cuidado, ya que si hay un campo en blanco / nulo (en el campo de código de mi código), el grupo contará eso como un registro. Sin embargo, una cláusula Where en la selección interna los ignorará. He puesto esto en mi blog, pero me preocupa que haya descubierto la respuesta con demasiada facilidad; otros parecen pensar que necesitas dos consultas secundarias para que esto funcione. ¿Es mi solución viable? Agrupaciones distintas en Access
Echa un vistazo a esta entrada de blog, parece que puedes hacerlo con subconsultas ...
http://blogs.msdn.com/access/archive/2007/09/19/writing-a-count-distinct-query-in-access.aspx
Lo propondría
select R_rep,sum(pp) as number_distinct_Billnos from (select R_rep, Billno, 1 as pp from `Vat_Sales` group by R_rep, Billno) t group by R_rep
prueba esto:
SELECT DISTINCT e.ETCode, t.TTitle, t.Tcomponent,
t.TImpliment_Partner, t.TVenue, t.TStartDate,
t.TEndDate, e.EDate, a.QCode, a.Answer,
q.SL, q.Question,
Count(a.Answer) AnswerCount,
Min(Select Count(*)
From (Select Distinct TCode From Training) As Z ) TCodeCount
FROM Evaluation As e
JOIN Training AS t ON e.ETCode=t.TCode
JOIN Answer AS a ON e.ECode=a.ECode
JOIN Questions AS q ON a.QCode=q.QCode
GROUP BY e.ETCode, a.QCode, t.TTitle, t.Tcomponent,
t.TImpliment_Partner, t.Tvenue, a.Answer, q.Question,
t.TStartDate, t.TEndDate, Evaluation.EDate, q.SL
ORDER BY a.QCode, a.Answer;