| 1 |
create table t1 |
| 2 |
( |
| 3 |
a int, b varchar(30), |
| 4 |
primary key(a) |
| 5 |
) engine = NDB; |
| 6 |
insert into t1 values (1,'one'); |
| 7 |
insert into t1 values (2,'two'); |
| 8 |
insert into t1 values (3,'three'); |
| 9 |
insert into t1 values (4,'four'); |
| 10 |
# |
| 11 |
# Group by query 1 |
| 12 |
# - group by |
| 13 |
# |
| 14 |
select sum(a), 'b' from t1 where a > 0 |
| 15 |
and b is not null group by substr(b,2) |
| 16 |
having sum(a) <> 2 ; |
| 17 |
sum(a) b |
| 18 |
1 b |
| 19 |
3 b |
| 20 |
4 b |
| 21 |
# |
| 22 |
# Group by query 2 |
| 23 |
# - union + group by |
| 24 |
select sum(a) + 200 as the_sum, 'City 1' as the_town from t1 |
| 25 |
group by b |
| 26 |
union distinct |
| 27 |
select sum(a) + 200, 'City 2' from t1 |
| 28 |
group by b ; |
| 29 |
the_sum the_town |
| 30 |
201 City 1 |
| 31 |
201 City 2 |
| 32 |
202 City 1 |
| 33 |
202 City 2 |
| 34 |
203 City 1 |
| 35 |
203 City 2 |
| 36 |
204 City 1 |
| 37 |
204 City 2 |
| 38 |
drop table t1; |