MS ACCESS:アクセスクエリを使用して個別の値をカウントするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1412442

  •  06-07-2019
  •  | 
  •  

質問

以下は現在の複雑なクエリです。

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;

別の列Training.TCodeがあります。個別のTraining.TCodeを数える必要がありますが、誰か助けていただけますか? さらに情報が必要な場合はお知らせください

役に立ちましたか?

解決

試用

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

編集-これを見てください...

次のSQLコードを取得します。最初の選択は、SQLサーバーがこれを行う方法であり、2番目のクエリはアクセスに準拠する必要があります...

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

次を返します。

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)

他のヒント

1年前にGoogleグループに同様の質問を投稿しました。素晴らしい回答を受け取りました:


クロスタブは(Steve Dassinの元の提案から)できる限り ファンド、サブファンドのいずれかをカウントすると:

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

各日(グループ)について、レコード数と 異なる(個別の)資金の数。

変更

PIVOT fund IN(Null)

to

PIVOT subfund IN(Null)

サブファンドについても同様に取得します。

それが役立つことを願って、 Vanderghast、Access MVP


それが機能するかどうかはわかりませんが、ここにその投稿へのリンクがあります。

Sadat、次のようなサブクエリを使用します:

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;

次の操作を行って、Accessで個別の値をカウントすることができました。

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

(私のフルーツフルーツフィールドに)空白/ヌルフィールドがある場合、グループがレコードとしてカウントするので注意する必要があります。ただし、内部選択のWhere句はそれらを無視します。 私はこれをブログに載せましたが、答えをあまりにも簡単に見つけてしまったことを心配しています-ここにいる他の人は、この作業を行うには2つのサブクエリが必要だと考えているようです。私のソリューションは実行可能ですか? Accessの個別のグループ

このブログエントリをご覧ください。サブクエリを使用してこれを行うことができます。...

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

提案します

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

これを試してください:

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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top