The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Help with join

Hello

I have a large table containing data about phone calls. i.e. when a phone call was made and who it was to. As a person may have multiple telephone numbers I use the following query to show me how many calls were made to that number per day.

Code:

select date, count(*)
from texts
where destination in (select number from peoplenos where peopleid = 2)
group by day(date), month(date), year(date)
order by day(date), year(date), month(date)

Obviously this outputs each day calls were made to this number and the number of calls. Obviously there will be no row when no calls were made.

What I want to be able to do is create a query which will combine this with data from up to about 5 other numbers giving me an output like below.

Date - #1 - #2 - #3
27/02 - 10 - 26 - 03
28/02 - 0 - 2 - 10

How can I achieve this? Any help will be gratefully appreciated.

I know I could do a view for each number I wanted to look at then then do a 2 way join on each of the views on the date column BUT the peopleid in the query will be generated dynamically by Java code so I cannot use static views.

So I have a table called people which just contains a list of names with an id. I have a table call peopleNos this has a list of phone numbers with a foreign key to the people table. One person may be associated with more than one telephone number. Then I have a table called texts and a table called calls.

So say I wanted the number of calls per day to people with id 1 and 2 (remembering that each person may have several phone numbers) I couldn't run the following query as if one person was associate with more than one phone number these would be counted separately.

Code:

select date, destination, count(*) from calls
where destination in (select number from peoplenos where peopleid = 1 or peopleid = 2)
group by destination, date
order by date

If I ran the following query I get the data I want but in the wrong format.

Code:

(select 1, date, count(*) from calls
where destination in (select number from peoplenos where peopleid = 1)
group by date
order by date)
union
(select 2, date, count(*) from calls
where destination in (select number from peoplenos where peopleid = 2)
group by date
order by date)