sql - first value in group by -
i want set column in group by table based on first (actually, only) value in group.
specifically, given table
id 1 t 1 t 2 f 3 t i want produce table
id multiplicity goodn 1 2 0 2 1 0 3 1 1 where goodn 1 if , if multiplicity 1 , good t:
select id, count(*) multiplicity, if (count(*) > 1, 0, if(good = 't', 1, 0)) goodn ... the question is: how extract first (in case, only) value of good group?
ps. there cheaper way test group has size 1 count(*)=1?
if count 1, both max(good) , min(good) "first" row in group.
select id, count(*) multiplicity, if (count(*) > 1, 0, if(max(good) = 't', 1, 0)) goodn ...
Comments
Post a Comment