Monday, 15 June 2015

sql - query multiple rows to summary result -



sql - query multiple rows to summary result -

i seek query of pass of fail result each id, table next in attachment, each part has 0 3 channel , has result.i need check each id if channel pass id, result passed, otherwise, failed

id result channel id1 fail 2 id1 pass 1 id1 fail 0 id2 pass 1 id2 pass 3 id2 pass 2 id2 pass 0 id3 fail 0 id3 pass 1 id3 pass 2 id3 fail 3 id4 fail 1 id4 pass 0 id4 pass 3 id4 fail 2

select id, result, count(channel) t grouping id, result having result = 'pass' , count(channel) = 3 union select id, result, count(channel) t grouping id, result having result = 'fail';

if not every id has 3 channels then

select id, result, count(channel) t t1 not exists (select 1 t t2 t1.id = t2.id , t2.result = 'fail') grouping id, result union select id, result, count(channel) t grouping id, result having result = 'fail';

for perfomance competition (if 'fail' < 'pass' in db, if it's not true utilize max):

select id, min(result) n t grouping id;

sql

No comments:

Post a Comment