2009-09-11 14 views
6

questa è la query complessa corrente indicata di seguito.ACCESSO MS: Come posso contare un valore distinto utilizzando la query di accesso?

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; 

C'è un'altra colonna Training.TCode. Devo contare distinti Training.TCode, qualcuno può aiutarmi? Se avete bisogno di ulteriori informazioni per favore fatemelo sapere

+0

si prega di non cercare di usare count (col distinti), perché MS Access non supporta count (distinti). Grazie per la tua collaborazione. – Sadat

risposta

4

provare

select ..., count(distinct Training.Tcode) as ..., ... 

EDIT - si prega ora guardate questo ...

Prendere il seguente codice SQL. Il primo di selezione è come SQL server sarebbe fare questo e la seconda query deve essere l'accesso compliant ...

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 

Si restituisce il seguente:

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) 
+0

hai provato? non funziona qui. – Sadat

+0

mi dispiace ignorami, ho perso il tag ACCESS – Rippo

+0

puoi ancora aiutarmi ... – Sadat

-1

provare questo:

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; 
+0

penso che non l'abbia provato o che non si preoccupi del database di accesso. in ms access count (distinct col) non è consentito, genera errore di sintassi come 'operatore mancante' – Sadat

+0

@Sadat, hai ragione, avevo dimenticato quanto sia diverso l'accesso SQL ... Quindi avrai bisogno di una subquery invece ... Ho modificato per includere che –

+0

ancora non funziona. prima di tutto devi usare esplicitamente "come" per gli alias. l'ho messo, ma c'è ancora errore. – Sadat

2

Ho pubblicato una domanda simile circa un anno fa nei gruppi di Google. Ho ricevuto una risposta eccellente:


A campi incrociati può fare (da una proposta originale di Steve Dassin) finché come si contano sia il fondo, sia il comparto:

TRANSFORM COUNT(*) AS theCell 
    SELECT ValDate, 
     COUNT(*) AS StandardCount, 
     COUNT(theCell) AS DistinctCount 
    FROM tableName 
    GROUP BY ValDate 
    PIVOT fund IN(Null) 

che, per ogni giorno (gruppo), restituirà il numero di record e il numero di diversi (distinti) fondi.

Change

PIVOT fund IN(Null) 

a

PIVOT subfund IN(Null) 

per ottenere lo stesso, per i comparti.

Sperando può aiutare, Vanderghast, accesso MVP


Non so se questo funzionerà, ma here's a link to that post.

+0

grazie in anticipo. lo controllerò APPENA POSSIBILE e lo lascerò sapere. – Sadat

2

Sadat, utilizzare una subquery come questo:

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; 
+1

Stavo per suggerire di creare una vista sulla tabella di allenamento completa del conteggio (*) secondo necessità. Quindi partecipa direttamente alla versione VIEW anziché alla tabella. Questo suggerimento farà la stessa cosa –

2

sono riuscito a fare un valore distinto conteggio in Access effettuando le seguenti operazioni:

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 

Bisogna stare attenti, come se non v'è un campo vuoto/nullo (nel mio campo frutto del codice) il gruppo conterà come un record.Una clausola Where nella selezione interna ignorerà comunque quelli. L'ho messo sul mio blog, ma sono preoccupato di aver scoperto la risposta troppo facilmente - altri qui sembrano pensare che siano necessarie due sottocategorie per fare in modo che funzioni. La mia soluzione è valida? Distinct groupings in Access

0

proporrei

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 
Problemi correlati