简体   繁体   English

查询SELECT DISTINCT count()

[英]Query SELECT DISTINCT count()

Hello there I have the following doubt I want to count how many times in a month I enter data. 您好,我有以下疑问,我想计算一个月内输入数据的次数。 My database is: 我的数据库是:

  • Date: 日期:
  • 10/2010 10/2010
  • 10/2010 10/2010
  • 09/2010 09/2010
  • 08/2010 08/2010

I have the following query. 我有以下查询。

 SELECT DISTINCT (date) FROM employee WHERE date IN (SELECT date FROM employee GROUP BY date HAVING count( date ) >0) ORDER BY date DESC; 

This query gives me: Date: 10/2017 8/2017 9/2017 这个查询给我:日期:10/2017 8/2017 9/2017

But I want you to give me something like that. 但是我要你给我这样的东西。

  • Count | 计数| Date 日期
  • 2 | 2 | 10/2017 二千零十七分之十
  • 1 | 1 | 9/2017 二千〇一十七分之九
  • 1 | 1 | 10/2017 二千零十七分之十

I hope I have explained my regards. 我希望我已经解释了我的问候。

You're overcomplicating it; 您太复杂了; no subquery, or DISTINCT, needed. 不需要子查询或DISTINCT。

SELECT `date`, count(*) 
FROM `employee`
GROUP BY `date`
HAVING count(*) > 0
ORDER BY `date` DESC;

I am a little confused as to what reason you would have for the HAVING count() > 0 though; 关于HAVING count() > 0原因,我有些困惑。 the only way something could have a zero count would mean it wasn't in the table (and therefore wouldn't show up anyway). 某件商品的唯一计数可能为零,这意味着该商品不在表中(因此无论如何也不会出现)。

Other observations: 其他观察:

  • DISTINCT is not a function; DISTINCT不是函数; enclosing the date in parenthesis in the SELECT clause has absolutely no effect. date括在SELECT子句的括号中绝对无效。 (Also, DISTINCT is almost never appropriate for a GROUPing query.) (此外,DISTINCT 几乎从不适合用于GROUPING查询。)
  • COUNT(somefield) is the same as COUNT(1) , COUNT(*) . COUNT(somefield)COUNT(1)COUNT(*) If you want the count of unique values you can do COUNT(DISTINCT somefield) ; 如果要计数唯一值,可以执行COUNT(DISTINCT somefield) ; but it wouldn't make sense to COUNT(DISTINCT groupingfield) as that would always result in 1. 但这对COUNT(DISTINCT groupingfield)毫无意义,因为它总是会导致1。

The query you wrote is a bit complicated. 您编写的查询有点复杂。 Distinct and group by are doing the same thing for you here. 唯一身份和分组依据在这里为您做同样的事情。 When you do a group by count will automatically give you the count of grouped rows. 当您按计数分组时,将自动为您提供分组行的计数。 Also you will have unique dates as well. 此外,您还将有独特的日期。 Try this. 尝试这个。

SELECT count(date), date
    FROM employee
GROUP BY date 
HAVING count( date ) >0
ORDER BY date DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM