Question : query with join on three tables and count in postgresql, how to do it ?

hello experts,

i need a query, with the following result
for example the number of usages per cards between first july and 15 july 2010. my rows would be like follow:
rf_card_id , account_id, firstname, lastname, nbr of usages,
 

i have tables as follow:
- user_person: user_person_id == account_id, firstname, lastname...
- cards: rf_card_id, account_id, .....
- trashes: trash_bag_id, rf_card_id, date, container_id.

what i succeed to do is only the cross join between user_personne and rf_cards, but how can i count and add the between ?

select c.rf_card_id, c.account_id, u.firstname, u.lastname
from rf_card as c Cross JOIN user_person as u
where c.account_id = u.user_person_id
order by rf_card_id asc;


thank you very much for your help.

Answer : query with join on three tables and count in postgresql, how to do it ?

I believe this should do it for you



select count(c.rf_card_id) as num_card, c.account_id, u.firstname, u.lastname
      from rf_card as c
            INNER JOIN user_person as u ON c.account_id = u.account_id
            INNER JOIN trashes as t ON c.rf_card_id = t.rf_card_id
WHERE date_trunc('day',t.date) between date_trunc('day', DATE '7/01/2010') AND date_trunc('day', DATE '7/15/2010')
group by c.account_id
Random Solutions  
 
programming4us programming4us