views:

195

answers:

4

How can I return what would effectively be a "contiguous" GROUP BY in MySQL. In other words a GROUP BY that respects the order of the recordset?

For example, SELECT MIN(col1), col2, COUNT(*) FROM table GROUP BY col2 ORDER BY col1 from the following table where col1 is a unique ordered index:

1    a
2    a
3    b
4    b
5    a
6    a

returns:

1    a    4
3    b    2

but I need to return the following:

1    a    2
3    b    2
5    a    2
+2  A: 

If the numbers in col1 are contiguous, you can do like this:

select x.col1, x.col2
from table x
left join table y on x.col1 = y.col1 + 1
where x.col2 <> isnull(y.col2, '')

It works like this:

-x-  -y-  out
1 a  - -  1 a
2 a  1 a
3 b  2 a  3 b
4 b  3 b
5 a  4 b  5 a
6 a  5 a
Guffa
As is, that will only return 2 a, 4 b - see my answer for corrections.
OMG Ponies
@rexem: Yes, I got the alignment wrong, and the comparison to null needed fixing.
Guffa
A: 

This won't work:

SELECT min_col1 = MIN(col1), col2
FROM table
GROUP BY col2
ORDER BY min_col1

Perhaps this?

SELECT min_col1, col2
FROM ( SELECT min_col1 = MIN(col1), col2
       FROM table
       GROUP BY col2 ) x
ORDER BY min_col1
Kenny Evitt
GROUP BY ignores order. I effectively need group by functionality that respects the order of the recordset.
Kuyenda
[The first query works in SQL Server.]
Kenny Evitt
+2  A: 

Use:

   SELECT MIN(t.id) 'mi', 
          t.val, 
          COUNT(*)
     FROM (SELECT x.id, 
                 x.val, 
                 CASE 
                   WHEN xt.val IS NULL OR xt.val != x.val THEN 
                     @rownum := @rownum+1 
                   ELSE 
                     @rownum 
                 END AS grp
            FROM TABLE x
            JOIN (SELECT @rownum := 0) r
       LEFT JOIN (SELECT t.id +1 'id',
                         t.val
                    FROM TABLE t) xt ON xt.id = x.id) t
 GROUP BY t.val, t.grp
 ORDER BY mi

The key here was to create an artificial value that would allow for grouping.

Previously, corrected Guffa's answer:

   SELECT t.id, t.val
     FROM TABLE t
LEFT JOIN TABLE t2 on t2.id + 1 = t.id
    WHERE t2.val IS NULL 
       OR t.val <> t2.val
OMG Ponies
transpose +1 to right side, then make it -1. so it will not yield table scan. mysql cannot have index on expression
Michael Buen
That's it! Many thanks!
Kuyenda
If you use `CASE WHEN xt.id IS NULL OR xt.val != x.val` then the SQL will not break when val is intentionally NULL, but it still captures the first offset comparison.
Kuyenda
Yes - because of the LEFT JOIN, you can check for any value in `xt` being null to know when on the first row, but subsequent comparisons have to be based on the value of the column you want to group on.
OMG Ponies
+1  A: 

same logic as rexem, but works on any windowing-capable RDBMS (won't work on MySQL yet):

CREATE TABLE tbl
(
id INT,
val VARCHAR(1)
);

INSERT INTO tbl(id,val) 
VALUES(1,'a'),(2,'a'),(3,'a'),(4,'a'),(5,'b'),(6,'b'),(7,'a'),(8,'a'),(9,'a');

source:

1 a
2 a
3 a
4 a
5 b
6 b
7 a
8 a
9 a

Windowing-style query: (works on windowing-capable rdbms):

WITH grouped_result AS
(
    SELECT x.id, x.val, 
        COUNT(CASE WHEN y.val IS NULL OR y.val <> x.val THEN 1 END) 
        OVER (ORDER BY x.id) AS grp
    FROM tbl x LEFT JOIN tbl y ON y.id + 1 = x.id
) 

SELECT MIN(id) mi, val, COUNT(*)
FROM grouped_result 
GROUP BY val, grp
ORDER BY mi

Output:

1  a  4
5  b  2
7  a  3

BTW, this is the result of the grouped_result without GROUP BY:

1  a  1
2  a  1
3  a  1
4  a  1
5  b  2
6  b  2
7  a  3
8  a  3
9  a  3

Feels good rewriting mysqlism-query to ANSI-conforming one :-) For now, while mysql don't have windowing capabality yet, rexem's answer is the best one. Rexem, that's a good mysql technique(JOIN (SELECT @rownum := 0)) there, and afaik MSSQL and PostgreSQL don't support implicitly declared variable, kudos! :-)

Michael Buen