This should sound stupid, but I never got why aggregate functions limit the records returned to 1. More than any practical reason, this is just to know.
id
1
2
3
4
Now SELECT id FROM table yields
id
1
2
3
4
If I do SELECT id, id, 1 AS p FROM table it gives
id id p
1 1 1
2 2 1
3 3 1
4 4 1
So next I assume SELECT id, MAX(2) AS p FROM table yields in
id p
1 2
2 2
3 2
4 2
But this actually gives:
id p
1 2
1) Why do this happen with aggregate functions and not give my expected result?
2) I found this with MySQL and SQLite. Do all databases respond the same?
3) Out of curiosity let me ask, how do I query to get a view like this:
id max(id)
1 4
2 4
3 4
4 4
1) You cannot execute this expression because ID is not in the aggregate function. Aggregate functions can only be used when they are the only column in the selection. This is because they return a single value and other columns mostly return more than one row.
2) I tried this in Access and got an error. (ID not in aggregate function…). I did not try this for other database systems yet.
3) You can get your result by adding GROUP BY ID at the end of the statement. This tells the system that you want to use each result of the aggregate function on every instance of ID. This enables you to use ID outside of the aggregate function.