sql - Join/merge 3 tables with count -
i have 3 tables:
people:
pid name 1 cal 2 example 3 4 person
talkingpoints:
tid pid talkingpoint 1 1 "..." 2 1 "..." 3 2 "..."
facts:
fid pid fact 1 3 "..." 2 2 "..."
and i'm trying combine count of talkingpoints , facts 'people', example:
pid name talkingpoints facts 1 cal 2 null 2 example 1 1 3 null 1 4 person null null
(ordered talkingpoints desc, alphabetical, including 'people' rows not have values counts)
i managed combine 'people' 1 other table:
select a.pid,a.name, count(b.tid) people a, talkingpoints b a.pid=b.pid group b.pid;
but query ignores rows 0 count (e.g. row 'person')
i hacked query works correctly 'talkingpoints' have not been able adapt combine 'facts' example table above.
select people.pid, people.name, x.talkingpoints people left join (select pid, name, count(name) talkingpoints (select people.pid, people.name people join talkingpoints on talkingpoints.pid = people.pid) talkingpoints group talkingpoints.pid) x on x.pid = people.pid order talkingpoints desc, people.name asc;
(probably terrible way worked in meantime)
how can adapt queries output table example?
select a.pid, a.name, count(distinct b.tid) talkingpoints, count(distinct c.fid) facts people left join talkingpoints b on a.pid = b.pid left join facts c on a.pid = c.pid group a.pid, a.name order a.pid
Comments
Post a Comment