📄 groupby.out
字号:
select i, dt, b from t group by i, dt, b order by i, dt, b;I |DT |B ---------------------------0 |1992-01-01|12af0 |1992-01-01|ffff0 |1992-09-09|12af1 |1992-01-01|12afNULL |NULL |NULLij> -- subset in same orderselect i, dt, b from t group by i, dt, b order by i, dt;I |DT |B ---------------------------0 |1992-01-01|ffff0 |1992-01-01|12af0 |1992-09-09|12af1 |1992-01-01|12afNULL |NULL |NULLij> -- different orderselect i, dt, b from t group by i, dt, b order by b, dt, i;I |DT |B ---------------------------0 |1992-01-01|12af1 |1992-01-01|12af0 |1992-09-09|12af0 |1992-01-01|ffffNULL |NULL |NULLij> -- subset in different orderselect i, dt, b from t group by i, dt, b order by b, dt;I |DT |B ---------------------------0 |1992-01-01|12af1 |1992-01-01|12af0 |1992-09-09|12af0 |1992-01-01|ffffNULL |NULL |NULLij> -- group by without having in from subqueryselect * from(select i, dt from t group by i, dt) t (t_i, t_dt),(select i, dt from t group by i, dt) m (m_i, m_dt)where t_i = m_i and t_dt = m_dt order by t_i,t_dt,m_i,m_dt;T_I |T_DT |M_I |M_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-09-09|0 |1992-09-091 |1992-01-01|1 |1992-01-01ij> select * from(select i, dt from t group by i, dt) t (t_i, t_dt),(select i, dt from t group by i, dt) m (m_i, m_dt)group by t_i, t_dt, m_i, m_dt order by t_i,t_dt,m_i,m_dt;T_I |T_DT |M_I |M_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-01-01|0 |1992-09-090 |1992-01-01|1 |1992-01-010 |1992-01-01|NULL |NULL 0 |1992-09-09|0 |1992-01-010 |1992-09-09|0 |1992-09-090 |1992-09-09|1 |1992-01-010 |1992-09-09|NULL |NULL 1 |1992-01-01|0 |1992-01-011 |1992-01-01|0 |1992-09-091 |1992-01-01|1 |1992-01-011 |1992-01-01|NULL |NULL NULL |NULL |0 |1992-01-01NULL |NULL |0 |1992-09-09NULL |NULL |1 |1992-01-01NULL |NULL |NULL |NULL ij> select * from(select i, dt from t group by i, dt) t (t_i, t_dt),(select i, dt from t group by i, dt) m (m_i, m_dt)where t_i = m_i and t_dt = m_dtgroup by t_i, t_dt, m_i, m_dt order by t_i,t_dt,m_i,m_dt;T_I |T_DT |M_I |M_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-09-09|0 |1992-09-091 |1992-01-01|1 |1992-01-01ij> select t.*, m.* from(select i, dt from t group by i, dt) t (t_i, t_dt),(select i, dt from t group by i, dt) m (t_i, t_dt)where t.t_i = m.t_i and t.t_dt = m.t_dtgroup by t.t_i, t.t_dt, m.t_i, m.t_dt order by t.t_i,t.t_dt,m.t_i,m.t_dt;T_I |T_DT |T_I |T_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-09-09|0 |1992-09-091 |1992-01-01|1 |1992-01-01ij> select t.t_i, t.t_dt, m.* from(select i, dt from t group by i, dt) t (t_i, t_dt),(select i, dt from t group by i, dt) m (t_i, t_dt)where t.t_i = m.t_i and t.t_dt = m.t_dtgroup by t.t_i, t.t_dt, m.t_i, m.t_dt order by t.t_i,t.t_dt,m.t_i,m.t_dt;T_I |T_DT |T_I |T_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-09-09|0 |1992-09-091 |1992-01-01|1 |1992-01-01ij> -- additional columns in group by list not in select listselect i, dt, b from t group by i, dt, b order by i,dt,b;I |DT |B ---------------------------0 |1992-01-01|12af0 |1992-01-01|ffff0 |1992-09-09|12af1 |1992-01-01|12afNULL |NULL |NULLij> select t.i from t group by i, dt, b order by i;I -----------0 0 0 1 NULL ij> select t.dt from t group by i, dt, b order by dt;DT ----------1992-01-011992-01-011992-01-011992-09-09NULL ij> select t.b from t group by i, dt, b order by b;B ----12af12af12afffffNULLij> select t.t_i, m.t_i from(select i, dt from t group by i, dt) t (t_i, t_dt),(select i, dt from t group by i, dt) m (t_i, t_dt)where t.t_i = m.t_i and t.t_dt = m.t_dtgroup by t.t_i, t.t_dt, m.t_i, m.t_dt order by t.t_i,m.t_i;T_I |T_I -----------------------0 |0 0 |0 1 |1 ij> -- having-- parameters in having clauseprepare p1 as 'select i, dt, b from t group by i, dt, b having i = ? order by i,dt,b';ij> execute p1 using 'values 0';IJ WARNING: Autocommit may close using result setI |DT |B ---------------------------0 |1992-01-01|12af0 |1992-01-01|ffff0 |1992-09-09|12afij> remove p1;ij> -- group by with having in from subqueryselect * from(select i, dt from t group by i, dt having 1=1) t (t_i, t_dt),(select i, dt from t group by i, dt having i = 0) m (m_i, m_dt)where t_i = m_i and t_dt = m_dt order by t_i,t_dt,m_i,m_dt;T_I |T_DT |M_I |M_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-09-09|0 |1992-09-09ij> select * from(select i, dt from t group by i, dt having 1=1) t (t_i, t_dt),(select i, dt from t group by i, dt having i = 0) m (m_i, m_dt)group by t_i, t_dt, m_i, m_dt order by t_i,t_dt,m_i,m_dt;T_I |T_DT |M_I |M_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-01-01|0 |1992-09-090 |1992-09-09|0 |1992-01-010 |1992-09-09|0 |1992-09-091 |1992-01-01|0 |1992-01-011 |1992-01-01|0 |1992-09-09NULL |NULL |0 |1992-01-01NULL |NULL |0 |1992-09-09ij> select * from(select i, dt from t group by i, dt having 1=1) t (t_i, t_dt),(select i, dt from t group by i, dt having i = 0) m (m_i, m_dt)where t_i = m_i and t_dt = m_dtgroup by t_i, t_dt, m_i, m_dthaving t_i * m_i = m_i * t_i order by t_i,t_dt,m_i,m_dt;T_I |T_DT |M_I |M_DT ---------------------------------------------0 |1992-01-01|0 |1992-01-010 |1992-09-09|0 |1992-09-09ij> -- correlated subquery in having clauseselect i, dt from tgroup by i, dthaving i = (select distinct i from tab1 where t.i = tab1.i) order by i,dt;I |DT ----------------------0 |1992-01-010 |1992-09-091 |1992-01-01ij> select i, dt from tgroup by i, dthaving i = (select i from t m group by i having t.i = m.i) order by i,dt;I |DT ----------------------0 |1992-01-010 |1992-09-091 |1992-01-01ij> -- column references in having clause match columns in group by listselect i as outer_i, dt from tgroup by i, dthaving i = (select i from t m group by i having t.i = m.i) order by outer_i,dt;OUTER_I |DT ----------------------0 |1992-01-010 |1992-09-091 |1992-01-01ij> -- additional columns in group by list not in select listselect i, dt from t group by i, dt order by i,dt;I |DT ----------------------0 |1992-01-010 |1992-09-091 |1992-01-01NULL |NULL ij> select t.dt from t group by i, dt having i = 0 order by t.dt;DT ----------1992-01-011992-09-09ij> select t.dt from t group by i, dt having i <> 0 order by t.dt;DT ----------1992-01-01ij> select t.dt from t group by i, dt having i != 0 order by t.dt;DT ----------1992-01-01ij> -- drop tablesdrop table t;0 rows inserted/updated/deletedij> drop table tab1;0 rows inserted/updated/deletedij> -- negative tests for selects with a having clause without a group by-- create a tablecreate table t1(c1 int, c2 int);0 rows inserted/updated/deletedij> -- binding of having clauseselect 1 from t1 having 1;ERROR 42X19: The WHERE or HAVING clause or CHECK CONSTRAINT definition is a 'INTEGER' expression. It must be a BOOLEAN expression.ij> -- column references in having clause not allowed if no group byselect * from t1 having c1 = 1;ERROR 42Y35: Column reference 'T1.C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> select 1 from t1 having c1 = 1;ERROR 42X04: Column 'C1' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'C1' is not a column in the target table.ij> -- correlated subquery in having clauseselect * from t1 t1_outer having 1 = (select 1 from t1 where c1 = t1_outer.c1);ERROR 42Y35: Column reference 'T1_OUTER.C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> -- drop the tabledrop table t1;0 rows inserted/updated/deletedij> -- bug 5653-- test (almost useful) restrictions on a having clause without a group by clause-- create the tablecreate table t1 (c1 float);0 rows inserted/updated/deletedij> -- populate the tableinsert into t1 values 0.0, 90.0;2 rows inserted/updated/deletedij> -- this is the only query that should not fail-- filter out all rowsselect 1 from t1 having 1=0;1 -----------ij> -- all 6 queries below should fail after bug 5653 is fixed-- select * select * from t1 having 1=1;ERROR 42Y35: Column reference 'T1.C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> -- select columnselect c1 from t1 having 1=1;ERROR 42Y35: Column reference 'C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> -- select with a built-in function sqrtselect sqrt(c1) from t1 having 1=1;ERROR 42Y35: Column reference 'C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> -- non-correlated subquery in having clauseselect * from t1 having 1 = (select 1 from t1 where c1 = 0.0);ERROR 42Y35: Column reference 'T1.C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> -- expression in select listselect (c1 * c1) / c1 from t1 where c1 <> 0 having 1=1;ERROR 42Y35: Column reference 'C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> -- betweenselect * from t1 having 1 between 1 and 2;ERROR 42Y35: Column reference 'T1.C1' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions. ij> -- drop the tabledrop table t1;0 rows inserted/updated/deletedij> -- bug 5920-- test that HAVING without GROUPBY makes one groupcreate table t(c int, d int);0 rows inserted/updated/deletedij> insert into t(c,d) values (1,10),(2,20),(2,20),(3,30),(3,30),(3,30);6 rows inserted/updated/deletedij> select avg(c) from t having 1 < 2;1 -----------2 ij> -- used to give several rows, now gives only oneselect 10 from t having 1 < 2;1 -----------10 ij> -- ok, gives one rowselect 10,avg(c) from t having 1 < 2;1 |2 -----------------------10 |2 ij> drop table t;0 rows inserted/updated/deletedij>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -