graefalexander
Programmer
Hello,
I do have two tables:
CREATE TABLE People
(
PeopleID INTEGER PRIMARY KEY,
LocationID INTEGER,
Name VARCHAR
);
PeopleID LocationID Name
---------------------------------------
1 1 Andreas
2 1 Alexander
3 1 Anette
4 1 Aaron
5 2 Babette
6 2 Balthasar
7 2 Barbara
8 3 Christian
9 3 Calvin
CREATE TABLE Location
(
LocationID INTEGER PRIMARY KEY,
LocationName VARCHAR
);
LocationID Name
---------------------------------------
1 Amsterdam
2 Bitburg
3 Chainsville
I want to know where everyone lives:
SELECT * FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
So I'll get a list with all people and their locations.
Now I want to know how many people live in each location:
SELECT Location.LocationName, COUNT(*)
FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
GROUP BY Location.LocationID
Now I'll get a list with all locations and a number telling how many
people live there.
However, I want a list with all people, their locations and a number
how many people live there. The only way I have found out was this
(actually doing both queries via subselect and joining them together):
SELECT * FROM People INNER JOIN
(SELECT Location.*, COUNT(*) AS LocationCount
FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
GROUP BY Location.LocationID) AS Location
ON People.LocationID = Location.LocationID
This works, I'll get a list with all people, their Locations and the number
how many people totaly live in this location. But if I want to filter, for
instance only on people with their name containing an "R", the thing gets
complicated because the WHERE-clause has to be repeated (I only want to know
how many people fullfilling the WHERE-clause live in the location):
SELECT * FROM People INNER JOIN
(SELECT Location.*, COUNT(*) AS LocationCount
FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
WHERE People.Name LIKE "%r%"
GROUP BY Location.LocationID) AS Location
ON People.LocationID = Location.LocationID
WHERE People.Name LIKE "%r%"
This also yields the right result set.
In the real-world application, the tables are much more complicated, data are several MBs,
the query filters on various rows, and the whole query takes about 800msecs, which is too
much for my application. So my question is:
Is there a combination of the first and the second query, that gives me a list containing
all people with their corresponding locations, and the number of people who live there, without
the overhead of my solution? I tried so many things, but couldnt find a solution.
Thank you in advance, Alex
I do have two tables:
CREATE TABLE People
(
PeopleID INTEGER PRIMARY KEY,
LocationID INTEGER,
Name VARCHAR
);
PeopleID LocationID Name
---------------------------------------
1 1 Andreas
2 1 Alexander
3 1 Anette
4 1 Aaron
5 2 Babette
6 2 Balthasar
7 2 Barbara
8 3 Christian
9 3 Calvin
CREATE TABLE Location
(
LocationID INTEGER PRIMARY KEY,
LocationName VARCHAR
);
LocationID Name
---------------------------------------
1 Amsterdam
2 Bitburg
3 Chainsville
I want to know where everyone lives:
SELECT * FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
So I'll get a list with all people and their locations.
Now I want to know how many people live in each location:
SELECT Location.LocationName, COUNT(*)
FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
GROUP BY Location.LocationID
Now I'll get a list with all locations and a number telling how many
people live there.
However, I want a list with all people, their locations and a number
how many people live there. The only way I have found out was this
(actually doing both queries via subselect and joining them together):
SELECT * FROM People INNER JOIN
(SELECT Location.*, COUNT(*) AS LocationCount
FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
GROUP BY Location.LocationID) AS Location
ON People.LocationID = Location.LocationID
This works, I'll get a list with all people, their Locations and the number
how many people totaly live in this location. But if I want to filter, for
instance only on people with their name containing an "R", the thing gets
complicated because the WHERE-clause has to be repeated (I only want to know
how many people fullfilling the WHERE-clause live in the location):
SELECT * FROM People INNER JOIN
(SELECT Location.*, COUNT(*) AS LocationCount
FROM People INNER JOIN Location
ON People.LocationID = Location.LocationID
WHERE People.Name LIKE "%r%"
GROUP BY Location.LocationID) AS Location
ON People.LocationID = Location.LocationID
WHERE People.Name LIKE "%r%"
This also yields the right result set.
In the real-world application, the tables are much more complicated, data are several MBs,
the query filters on various rows, and the whole query takes about 800msecs, which is too
much for my application. So my question is:
Is there a combination of the first and the second query, that gives me a list containing
all people with their corresponding locations, and the number of people who live there, without
the overhead of my solution? I tried so many things, but couldnt find a solution.
Thank you in advance, Alex