GROUP BY with only first row of sequence of one column?

RobM

at first here is the alpha version of what I want: http://sqlfiddle.com/#!2/45c89/2

However I don't want to count all representative_id, but only this rows with the lowest id, eg:

(`id`, `economy_id`, `representative_id`)
(1, 1, 5), <-this one, lowest id through the 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 the 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 the same economy_id=3
(18, 3, 4),
(19, 3, 1),
(20, 3, 2),
(21, 3, 102),
(22, 4, 1), <-this one, lowest id through the same economy_id=4
(23, 4, 2),
(24, 4, 102),
(25, 5, 1),  <-this one, lowest id through the same economy_id=5
(26, 5, 2),
(27, 5, 102),
(28, 5, 7),
(29, 6, 1),  <-this one, lowest id through the same economy_id=6

The output should be:

representative_id, count()

According to above example:

5, 1
7, 1
3, 1
1, 3

Is it possible only in SQL?

sgeddes

If I'm understanding your question correctly, I think this should work using min in a subquery and joining back to itself:

select s.representative_id, count(*)
from stl_parliament s
  join 
  (
    select min(id) minid
    from stl_parliament
    group by economy_id
  ) t on s.id = t.minid
group by s.representative_id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GROUP BY with only first row of sequence of one column?

From Dev

Only one column after group by()

From Dev

Oracle group by only ONE column

From Dev

How to select the first two row of each group and count difference between them in one column?

From Dev

Select top N rows, first max row from each group distinct by one column (Spring,Hibernate,JPQL)

From Dev

jqgrid select only one row per group

From Dev

Column added only works for first row in GridView

From Dev

How to display first column and row only?

From Dev

MySQL - if row is duplicate, return only the first one

From Dev

Group several rows of data into one row by column?

From Dev

how to group by one column only using linq?

From Dev

how to group by one column only using linq?

From Dev

Sum only visible cells in one ROW (not column)?

From Dev

ASP .NET RowDataBound attribute working for only one row (first one)

From Dev

How to apply a function only to the first row of a group in dplyr?

From Dev

Group by and only first row should have value in data frame

From Dev

Show only first row of each group excluding NULL

From Dev

Show only first row within a group for Microsoft SQL Server

From Dev

Query using group_concat is returning only one row

From Dev

how do i use group by in only one row?

From Dev

Query using group_concat is returning only one row

From Dev

mysql query on column "serialNo" only returns the first row

From Dev

Highlight ONLY THE FIRST of all min/max values in a row/column in Excel

From Dev

Mysql query to only return rows based on column of first row

From Dev

Why is csv.Dictreader reading only the first row/column?

From Dev

how to select only one row from all columns but the first

From Dev

awk/sed to compare first column and add in subsequent lines in one row

From Dev

Returning first row of group

From Dev

First row for each group

Related Related

  1. 1

    GROUP BY with only first row of sequence of one column?

  2. 2

    Only one column after group by()

  3. 3

    Oracle group by only ONE column

  4. 4

    How to select the first two row of each group and count difference between them in one column?

  5. 5

    Select top N rows, first max row from each group distinct by one column (Spring,Hibernate,JPQL)

  6. 6

    jqgrid select only one row per group

  7. 7

    Column added only works for first row in GridView

  8. 8

    How to display first column and row only?

  9. 9

    MySQL - if row is duplicate, return only the first one

  10. 10

    Group several rows of data into one row by column?

  11. 11

    how to group by one column only using linq?

  12. 12

    how to group by one column only using linq?

  13. 13

    Sum only visible cells in one ROW (not column)?

  14. 14

    ASP .NET RowDataBound attribute working for only one row (first one)

  15. 15

    How to apply a function only to the first row of a group in dplyr?

  16. 16

    Group by and only first row should have value in data frame

  17. 17

    Show only first row of each group excluding NULL

  18. 18

    Show only first row within a group for Microsoft SQL Server

  19. 19

    Query using group_concat is returning only one row

  20. 20

    how do i use group by in only one row?

  21. 21

    Query using group_concat is returning only one row

  22. 22

    mysql query on column "serialNo" only returns the first row

  23. 23

    Highlight ONLY THE FIRST of all min/max values in a row/column in Excel

  24. 24

    Mysql query to only return rows based on column of first row

  25. 25

    Why is csv.Dictreader reading only the first row/column?

  26. 26

    how to select only one row from all columns but the first

  27. 27

    awk/sed to compare first column and add in subsequent lines in one row

  28. 28

    Returning first row of group

  29. 29

    First row for each group

HotTag

Archive