mfmz
mfmz

Reputation: 227

Different outputs from same GROUP BY SQL statement

I have tried running two SQL queries which are basically the same but only differ in terms of the GROUP BY. To make it clearer, the following are my queries.

First query:

SELECT EXTRACT(YEAR FROM tarikh_lulus) AS Tahun, COUNT(*) AS Jumlah 
FROM association 
GROUP BY Tahun

The first query gave me the following result

Tahun   Jumlah
0   60
0   1464
5   1
6   2
7   1
8   2
1970    3
1998    431
1999    206
2000    77
2001    97
2002    104
2003    123
2004    59
2005    1
2006    8
2007    162
2008    173
2009    498
2010    620
2011    915
2012    911
2013    602

The second query :

SELECT EXTRACT(YEAR FROM tarikh_lulus) AS Tahun, COUNT(*) AS Jumlah 
FROM association 
GROUP BY tarikh_lulus

The second query gave the following:

Tahun   Jumlah
0   60
1970    3
1998    16
1998    1
1998    36
1998    1
1999    8
1999    14
1999    3
1999    12
2000    1
2000    5
2001    4
2001    1
2001    7
2002    3
2002    2
2002    1
2002    3
2003    7
2003    3
2003    1
2004    1
2005    1
2007    1
2007    1
2007    2
2007    1
2008    1
2008    2
2008    1
2008    11
2009    1
2009    26
2009    4
2010    47
2010    71
2010    6
2010    52
2010    1
2011    7
2011    32
2011    10
2011    15
2011    1
2011    1
2011    38
...

Why is it that the results are different? As far as I can understand it is grouped by the same column?

Upvotes: 0

Views: 71

Answers (1)

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

Well, in first query, you group by year.

So you will get one result line by year.

In second, you group by a complete date.

So you will get one result line by complete date, even if you just SELECT the year (it's just display)

Difference

There's only one year per year.

They are many dates in a year.

Upvotes: 4

Related Questions