mysql - I can not do this query without nested select -


i have table 3 attributes (identificator, id_law, surname, vote). vote can take 3 values: positive, against or abstained.

i can not query: want know every law there votes in positive, against , abstentions.

the select must unique , not allowed nested select

create table house_law (   -- proposed vote law_id varchar(50) not null primary key, descr varchar(255) );  insert house_law (law_id,descr) values ('hr-109b','forbid driving near turtles');  create table house_rep (   -- session monday friday, captures start date rep_id int not null primary key, fullname varchar(80) not null, party varchar(80) );  insert house_rep (rep_id,fullname,party) values (1001,'thomas jefferson','whig'); insert house_rep (rep_id,fullname,party) values (800,'fred','abstain party'); insert house_rep (rep_id,fullname,party) values (700,'stan','abstain party');  create table votes_cast (law_id varchar(50) not null, rep_id int not null, the_vote varchar(20) -- for, against, abstain );  insert votes_cast (law_id,rep_id,the_vote) values ('hr-109b',1001,'against'); insert votes_cast (law_id,rep_id,the_vote) values ('hr-109b',800,'abstain'); insert votes_cast (law_id,rep_id,the_vote) values ('hr-109b',700,'abstain');  -- results  select l.law_id, l.descr, sum(case when v.the_vote='for' 1 else 0 end) for_votes, sum(case when v.the_vote='against' 1 else 0 end) against_votes, sum(case when v.the_vote='abstain' 1 else 0 end) abstain_votes house_law l join votes_cast v on v.law_id=l.law_id group l.law_id -- or close 1 law in particular 

output: id | descr | votes_for | against | abstain

hr-109b forbid driving near turtles 0 1 2


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -