当前位置: 数据库>oracle
oracle group by语句实例测试
来源: 互联网 发布时间:2014-09-07
本文导语: Sql代码 代码如下: CREATE TABLE test(a VARCHAR2( 20),b VARCHAR2 (20),c NUMBER,d VARCHAR2 (20)) INSERT INTO test VALUES( 'a1','b1' ,1, 't'); INSERT INTO test VALUES( 'a3','b3' ,3, 't'); INSERT INTO test VALUES( 'a4','b4' ,4, 't'); INSERT INTO test VALUES( 'a2','b2' ,2, 't'); INSERT INTO test...
Sql代码
CREATE TABLE test(a VARCHAR2( 20),b VARCHAR2 (20),c NUMBER,d VARCHAR2 (20))
INSERT INTO test VALUES( 'a1','b1' ,1, 't');
INSERT INTO test VALUES( 'a3','b3' ,3, 't');
INSERT INTO test VALUES( 'a4','b4' ,4, 't');
INSERT INTO test VALUES( 'a2','b2' ,2, 't');
INSERT INTO test VALUES( 'xx','xx' ,5, 'x');
INSERT INTO test VALUES( 'a1','b1' ,1, 't');
INSERT INTO test VALUES( 'a3','b3' ,3, 't');
INSERT INTO test VALUES( 'a4','b4' ,4, 't');
INSERT INTO test VALUES( 'a2','b2' ,2, 't');
INSERT INTO test VALUES( 'xx','xx' ,5, 'x');
COMMIT;
1.SELECT a,b, SUM(c) FROM test WHERE d= 't' GROUP BY a,b ORDER BY a,b;
2.SELECT a,b, SUM(c) FROM test WHERE d= 't' GROUP BY b,a ORDER BY a,b;
总结:对于1,2而言,结果应该是一样的,但是分组的顺序不同,SQL的性能应该也是不同的。
代码如下:
CREATE TABLE test(a VARCHAR2( 20),b VARCHAR2 (20),c NUMBER,d VARCHAR2 (20))
INSERT INTO test VALUES( 'a1','b1' ,1, 't');
INSERT INTO test VALUES( 'a3','b3' ,3, 't');
INSERT INTO test VALUES( 'a4','b4' ,4, 't');
INSERT INTO test VALUES( 'a2','b2' ,2, 't');
INSERT INTO test VALUES( 'xx','xx' ,5, 'x');
INSERT INTO test VALUES( 'a1','b1' ,1, 't');
INSERT INTO test VALUES( 'a3','b3' ,3, 't');
INSERT INTO test VALUES( 'a4','b4' ,4, 't');
INSERT INTO test VALUES( 'a2','b2' ,2, 't');
INSERT INTO test VALUES( 'xx','xx' ,5, 'x');
COMMIT;
1.SELECT a,b, SUM(c) FROM test WHERE d= 't' GROUP BY a,b ORDER BY a,b;
2.SELECT a,b, SUM(c) FROM test WHERE d= 't' GROUP BY b,a ORDER BY a,b;
总结:对于1,2而言,结果应该是一样的,但是分组的顺序不同,SQL的性能应该也是不同的。