sql - How can I SUM a COUNT(*) subquery to find two string values per ID? -
i'm trying build sql server query spit out numbers. specifically, need know how many people both pizza , soda displayed showing proper count relationship.
i have 2 tables this:
people
:
1 andy relative 2 jim friend 3 anderson friend 4 pamela relative
likes
:
1 pizza 1 soda 2 pizza 3 soda 4 pizza 4 soda
my desired output following. list of how many relatives , how many friends pizza , soda:
relative 2 friend 0
this pretty i'm @ wholly incorrect:
select relation, (select count(*) likes lik id = lik.id , pizzavalue = 'pizza') + (select count(*) likes lik id = lik.id , pizzavalue = 'soda') dbo.people inner join likes lik on ps.id = lik.id
try:
select relation, count(*) people p join likes l1 on l1.id = p.id , pizzavalue = 'pizza' join likes l2 on l2.id = p.id , pizzavalue = 'soda' group relation
this first join person table likes table , leave every person likes both soda , pizza. group return single count per relation.
Comments
Post a Comment