Consider the following:
CREATE TABLE IF NOT EXISTS person (
id BIGSERIAL,
favorite_color TEXT,
PRIMARY KEY (id)
);
INSERT INTO person (favorite_color) VALUES ('red');
INSERT INTO person (favorite_color) VALUES ('green');
INSERT INTO person (favorite_color) VALUES ('blue');
INSERT INTO person (favorite_color) VALUES ('blue');
INSERT INTO person (favorite_color) VALUES ('blue');
I want to now query the table to give me a COUNT for all the rows where favorite_color = blue AND return all the rows. How can I achieve the following output with a single postgresql query?
{
"total" 3,
"results": [
{"id": 3, "favorite_color": "blue"},
{"id": 4, "favorite_color": "blue"},
{"id": 5, "favorite_color": "blue"},
]
}
I know how to write SQL statements, I am asking if it is possible to write a single SQL statement to achieve my desired result, oppose to hitting the database twice (once for the count, second for results).