Question: I am aware of the memory alignment in structure but I am stumped at this implementation I came across in a project I am working on.

```
struct default {
     uint8_t variable[((sizeof(struct dummyStructure) + 3) /4)*4]  // Align on 32 bit boundary
}

```

It is more like a black box testing for me at the moment because I dont have access to the functions, but can anyone explain the math used here to cause this alignment to happen.

Answer: 
```
select      id
           ,status

from       (select      status
                       ,id
                       ,@prev_status := @status
                       ,@status      := status
                       ,@rn          := case when @prev_status = status 
                                             then @rn + 1 
                                             else 1 
                                        end           as rn

            from        mytable t1,(select @status:=null,@rn:=0) x

            order by    status
                       ,id
            ) t

 order by  floor((rn-1) / case status when 'a' then 2 else 1 end)         
          ,case status when 'a' then 1 else 2 end  
          ,rn
;

```

---

```
+----+--------+
| id | status |
+----+--------+
|  1 | a      |
|  5 | a      |
|  2 | b      |
|  6 | a      |
|  8 | a      |
|  3 | b      |
|  4 | b      |
|  7 | b      |
+----+--------+

```

**This will help you to understand the solution:**  

(group\_id = `floor((rn-1) / case status when 'a' then 2 else 1 end)`)

```
+--------+----+----+----------+
| status | id | rn | group_id |
+--------+----+----+----------+
| a      |  1 |  1 |        0 |
| a      |  5 |  2 |        0 |
| a      |  6 |  3 |        1 |
| a      |  8 |  4 |        1 |
| b      |  2 |  1 |        0 |
| b      |  3 |  2 |        1 |
| b      |  4 |  3 |        2 |
| b      |  7 |  4 |        3 |
+--------+----+----+----------+

```
Use variables to enumerate the values and then some simple logic:

```
select id, status
from (select t.*,
             (@rn := if(@s = status, @rn + 1,
                        if(@s := status, 1, 1)
                       )
             ) as rn
      from t cross join
           (select @rn := 0, @s := '') params
      where status in ('a', 'b')
      order by status, id
     ) ab
order by (case when status = a then floor( (rn - 1) / 2) else (rn - 1) end),
         status, id;

```