Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql MS ACCESS:如何使用ACCESS查询计算不同的值?_Sql_Ms Access_Count_Distinct - Fatal编程技术网

Sql MS ACCESS:如何使用ACCESS查询计算不同的值?

Sql MS ACCESS:如何使用ACCESS查询计算不同的值?,sql,ms-access,count,distinct,Sql,Ms Access,Count,Distinct,下面是当前的复杂查询 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],

下面是当前的复杂查询

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.t代码,有人能帮我吗? 如果您需要更多信息,请告诉我

select ..., count(distinct Training.Tcode) as ..., ...
编辑-请现在查看此…

以下面的SQL代码为例。第一个选择是SQL server将如何执行此操作,第二个查询应与access兼容

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)
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
试试这个:

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;

看看这篇博文,似乎你可以通过子查询来实现这一点


大约一年前,我在谷歌集团发布了一个类似的问题。我得到了一个极好的回答:


交叉表可以做到(根据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)

同样,对于子基金

希望能有所帮助, Vanderghast,访问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中计算了不同的值:

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)
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
您必须小心,因为如果有一个空白/空字段(在我的代码结果字段中),则group by会将其作为记录计算。但是,内部select中的Where子句将忽略这些内容。 我已经在我的博客上发表了这篇文章,但我担心我太容易找到答案了——这里的其他人似乎认为需要两个子查询来实现这一点。我的解决方案可行吗? 我建议

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

我想你们并没有试过,或者根本不关心access数据库。在ms中,不允许使用访问计数(distinct col),它会抛出语法错误为“缺少运算符”@Sadat,你是对的,我忘记了访问SQL有多么不同。。。然后您将需要一个子查询来代替。。。我已经编辑过了,但仍然不起作用。首先,必须显式地将“as”用作别名。我已经放置了它,但仍然存在错误。@Sadat,我编辑添加了“As”,并在子查询周围添加了聚合函数,但您得到了什么错误?Jet/ACE SQL中的表别名不需要As,因此我认为这可能不是问题的原因。抱歉,忽略我,我错过了访问标签抱歉,我正在复习我的访问技能。。。。看这里,这表明您只能通过使用两个子查询进行计数(不同)。。。希望这有帮助。萨达特,看看我编辑的版本。这将使您达到您需要的位置。请不要尝试使用计数(distinct col),因为MS Access不支持计数(distinct)。感谢您的合作。我打算建议您根据需要查看培训表,并填写您的计数(*)。然后在视图版本上而不是直接在表上进行连接。这个建议也会起到同样的作用。