sql - Postgres LEFT JOIN with SUM, missing records -
I am trying to get some sort of records in a related table. I'm using a left join.
So I have a query that is not quite right and the returning to the right result is the highest performance cost in the query of the right results. ID wants to use the first approach, if I can correct the results (see)
make the table people (id serial, name varchar not null); Table domestication (id serial, not vancar faucet name, type varchar no redundant, live boolean faucet default false, do not tap person_id integer); Include people (name) values ('Chad'), ('Buck'); Living in animals (name, living, compassionate, person_id) values ('dog', true, 'dog', 1), ('dog master flash', true, 'dog', 1), ('catio' True, 'cat', 1), ('lucky', false, 'cat', 2);
My goal is to withdraw a table with all the people and calculate the varieties of animals that they have:
| ID | ALIVE_DOGS_COUNT | ALIVE_CATS_COUNT | | ---- | ------------------ | ------------------ | | 1 | 2 | 1 | | 2 | 0 | 0 |
I have made the example even more trivial in our production app (actually not a pet), I have very poor per 100,000 dead dogs and cats per person, but this example is for relay It is easy;) I was hoping to filter all the 'dead' stuff before counting. I now have a slow question in production (above sqlfiddle), but LEFT JOIN version would be great to work.
Usually the fastest if you get all or more rows : < / P>
Comments
Post a Comment