mysql - GROUP BY with only first row of sequence of one column? -
at first here alpha version of want: http://sqlfiddle.com/#!2/45c89/2
however don't want count representative_id, rows lowest id, eg:
(`id`, `economy_id`, `representative_id`) (1, 1, 5), <-this one, lowest id through same economy_id=1 (2, 1, 6), (3, 1, 7), (4, 1, 8), (5, 1, 3), (6, 1, 4), (7, 1, 1), (8, 1, 2), (9, 1, 102), (10, 2, 7), <-this one, lowest id through same economy_id=2 (11, 2, 8), (12, 2, 102), (13, 2, 1), (14, 2, 2), (15, 2, 3), (16, 2, 4), (17, 3, 3), <-this one, lowest id through same economy_id=3 (18, 3, 4), (19, 3, 1), (20, 3, 2), (21, 3, 102), (22, 4, 1), <-this one, lowest id through same economy_id=4 (23, 4, 2), (24, 4, 102), (25, 5, 1), <-this one, lowest id through same economy_id=5 (26, 5, 2), (27, 5, 102), (28, 5, 7), (29, 6, 1), <-this one, lowest id through same economy_id=6
the output should be:
representative_id, count()
according above example:
5, 1 7, 1 3, 1 1, 3
is possible in sql?
if i'm understanding question correctly, think should work using min
in subquery
, joining itself:
select s.representative_id, count(*) stl_parliament s join ( select min(id) minid stl_parliament group economy_id ) t on s.id = t.minid group s.representative_id
Comments
Post a Comment