Here is a table1:
+--------------------+
| type | size | code |
+--------------------+
| A | 10 | 1 |
| A | 8 | 0 |
| B | 3 | 1 |
| B | 5 | 0 |
| A | 9 | 1 |
+--------------------+
Now I want to create a view to reach these goals:
1. Group by "type" column
2. calculate avg(size) for each "type"
3. calculate the percent of "1" in "code" cloumn for "type"
+----------------------------+
| type | avg(size) | percent |
+----------------------------+
| A | 9 | 2/3 |
| B | 4 | 1/2 |
+----------------------------+
What is the best way to create the view like this?
Try this one,
SQLFiddle demo