【问题标题】:MS ACCESS: How can i count distinct value using access query?MS ACCESS:如何使用访问查询计算不同的值?
【发布时间】:2010-11-27 14:24:22
【问题描述】:

这是下面给出的当前复杂查询。

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,有人可以帮助我吗? 如果您需要更多信息,请告诉我

【问题讨论】:

  • 请不要尝试使用 count(distinct col),因为 MS Access 不支持 count(distinct)。感谢您的合作。

标签: sql ms-access count distinct


【解决方案1】:

试试

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

编辑 - 现在请看这个...

采用以下 SQL 代码。第一个选择是 SQL 服务器将如何执行此操作,第二个查询应该是访问兼容...

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)

【讨论】:

  • 抱歉忽略我,我错过了 ACCESS 标签
  • 对不起,我的访问技能复习了......看这里blogs.msdn.com/access/archive/2007/09/19/…这表明你只能通过使用两个子查询来计数(不同)......希望这会有所帮助。跨度>
  • 萨达特,看我编辑的版本。这应该能让你到达你需要的地方。
【解决方案2】:

大约一年前,我在 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)

PIVOT subfund IN(Null)

为子基金获得相同的收益。

希望对你有帮助 Vanderghast,访问 MVP


我不知道这是否可行,但here's a link to that post

【讨论】:

    【解决方案3】:

    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;
    

    【讨论】:

    • 我建议根据需要使用您的计数 (*) 在训练表上创建一个完整的视图。然后直接加入 VIEW 版本而不是表。这个建议会做同样的事情。
    【解决方案4】:

    我设法通过执行以下操作在 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 子句将忽略这些。 我已经把它放在我的博客上,但我担心我太容易找到答案——这里的其他人似乎认为你需要两个子查询才能完成这项工作。我的解决方案可行吗? Distinct groupings in Access

    【讨论】:

      【解决方案5】:

      看看这个博客条目,看来你可以用子查询来做到这一点......

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

      【讨论】:

        【解决方案6】:

        我会提议

        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
        

        【讨论】:

          【解决方案7】:

          试试这个:

          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;
          

          【讨论】:

          • 我认为您没有尝试过或不关心访问数据库。在 ms 访问计数(distinct col)中是不允许的,它会抛出语法错误为“缺少运算符”
          • @Sadat,你是对的,我忘记了 Access SQL 的不同之处......然后你需要一个子查询......我已经编辑包含它
          • 这仍然不起作用。首先,您必须明确使用“as”作为别名。我已经放置了,但仍然存在错误。
          • @Sadat,我编辑添加了'As's,并在子查询周围添加了一个聚合函数,但是你得到了什么错误?
          • Jet/ACE SQL 中的表别名不需要 AS,所以我认为这不是问题的原因。
          猜你喜欢
          • 1970-01-01
          • 2017-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多