使用GROUP BY进行COUNT DISTINCT

时间:2017-05-31 18:24:32

标签: mysql count distinct

我有两个关于用户数量的mySQL查询:

首先是:

public class Main {

  public static void main(String[] args) {

    String playerName = "Piotr";

    int playerPosition = CalculateHighScorePosition(1500);
    displayHighScorePosition("Piotr", playerPosition);

    playerPosition = CalculateHighScorePosition(900);
    displayHighScorePosition("Cezary", playerPosition);

    playerPosition = CalculateHighScorePosition(400);
    displayHighScorePosition("Adam", playerPosition);

    playerPosition = CalculateHighScorePosition(50);
    displayHighScorePosition("Julia", playerPosition);
  }

  private static void displayHighScorePosition(String playerName, int playerPosition) {
    System.out.println( playerName + " managed to get into " + playerPosition );
  }

  private static int CalculateHighScorePosition(int playerScore) {
    if (playerScore > 1000) {
        return 1;
    }
    else if (playerScore > 500 && playerScore < 1000) {
        return 2;
    }
    else if (playerScore > 100 && playerScore < 500) {
        return 3;
    }
    else {
        return 4;
    }
  }

}

结果:

SELECT
date_format(r.dataZalozenia,'%Y-%m') as rok_miesiac,
count(distinct r.idUsera) as liczba_buyers

FROM
rezerwacje r

WHERE
r.dataZalozenia >= '2017-01-01'
and r.status = 'zabookowana'

GROUP BY
1

第二个是:

"rok_miesiac"   "liczba_buyers"
"2017-01"   "12862"
"2017-02"   "10208"
"2017-03"   "10189"
"2017-04"   "10993"
"2017-05"   "10705"

结果:

SELECT DISTINCT
r.idUsera,
count(r.idUsera) as l_reze

FROM
rezerwacje r

WHERE
r.dataZalozenia between '2017-04-01' and '2017-04-30'
and r.status = 'zabookowana'

GROUP BY
r.idUsera

ORDER BY
l_reze DESC

等。即基本上是user_id和预订数量。在这种情况下,我得到10768行,即10768个不同的user_id(仅限4月份)。

为什么4月1日查询返回10993个不同的user_id(我认为),而第二个查询返回10768?

1 个答案:

答案 0 :(得分:1)

如果dataZalozenia是日期时间或时间戳,则您的BETWEEN将不包括结束日期的大多数记录; &#39; 2017年4月30日&#39;被视为&#39; 2017-04-30 00:00:00&#39;。