tags:

views:

61

answers:

2

Hello.

If i perform a standard query in SQLite:

SELECT * FROM my_table

I get all records in my table as expected. If i perform following query:

SELECT *, 1 FROM my_table

I get all records as expected with rightmost column holding '1' in all records. But if i perform the query:

SELECT *, COUNT(*) FROM my_table

I get only ONE row (with rightmost column is a correct count). Why is such results? I'm not very good in SQL, maybe such behavior is expected? It seems very strange and unlogical to me :(.

+3  A: 

SELECT *, COUNT(*) FROM my_table is not what you want, and it's not really valid SQL, you have to group by all the columns that's not an aggregate.

You'd want something like

SELECT somecolumn,someothercolumn, COUNT(*) 
   FROM my_table 
GROUP BY somecolumn,someothercolumn
nos
SQLite help on aggregate functions (sqlite.org/lang_aggfunc.html) don't have anything about 'group by' :(. Is it some documentation i can read about this restriction for better understanding?
Eye of Hell
@Eye of Hell any book on SQL would do, it's nothing sqlite specific.
nos
+3  A: 

count(*) is an aggregate function. Aggregate functions need to be grouped for a meaningful results. You can read: http://stackoverflow.com/questions/2698108/count-columns-group-by

Burçin Yazıcı
SQLite help on aggregate functions (http://www.sqlite.org/lang_aggfunc.html) don't have anything about 'group by' :(. Is it some documentation i can read about this restriction for better understanding?
Eye of Hell
The SQLite documentation does say "in a group" / "in the group". I suggest to read a tutorial or a book about SQL.
Thomas Mueller