Domanda

ecco l'attuale complessa query fornita di seguito.

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;

Esiste un'altra colonna Training.TCode. Devo contare su Training.TCode distinto, qualcuno può aiutarmi? Se hai bisogno di ulteriori informazioni, faccelo sapere

È stato utile?

Soluzione

try

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

MODIFICA - per favore ora guarda questo ...

Prendi il seguente codice SQL. La prima selezione è come farebbe SQL Server e la seconda query dovrebbe essere conforme all'accesso ...

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

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)

Altri suggerimenti

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


Una tabella a campi incrociati può fare (da una proposta originale di Steve Dassin) per tutto il tempo mentre conti il ??fondo, o 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 fondi diversi (distinti).

Cambia

PIVOT fund IN(Null)

a

PIVOT subfund IN(Null)

per ottenere lo stesso, per i comparti.

Sperando che possa aiutare, Vanderghast, Accedi a MVP


Non so se funzionerà, ma ecco un link a quel post .

Sadat, usa una sottoquery come questa:

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;

Sono riuscito a contare un valore distinto in Access facendo quanto segue:

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

Devi stare attento come se ci fosse un campo vuoto / nullo (nel mio campo frutto del mio codice) il gruppo lo considererà come un record. Una clausola Where nella selezione interna ignorerà tuttavia quelle. L'ho messo sul mio blog, ma sono preoccupato di aver scoperto la risposta troppo facilmente - altri qui sembrano pensare che siano necessarie due sottoquery per farlo funzionare. La mia soluzione è praticabile? Raggruppamenti distinti in Access

Dai un'occhiata a questo post sul blog, sembra che tu possa farlo con le subquery ....

http://blogs.msdn.com/access/archive/2007/09/19/writing-a-count-distinct-query-in-access.aspx

Vorrei proporre

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

prova 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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top