sqlite/test/aggorderby.test
drh d5e040b6f0 Add string_agg(X,Y) as an alias for group_concat(X,Y), for compatibility
with SQLServer and PG.

FossilOrigin-Name: b91c19bf2680f60d7826ab5d9e7902e2dc2a55d847bbea565a6489d47f2cc8f1
2023-10-20 20:19:30 +00:00

80 lines
2.7 KiB
Plaintext

# 2023-10-18
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements tests for ORDER BY on aggregate functions.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
do_execsql_test aggorderby-1.1 {
CREATE TABLE t1(a TEXT,b INT,c INT,d INT);
WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<9)
INSERT INTO t1(a,b,c,d) SELECT printf('%d',(x*7)%10),1,x,10-x FROM c;
INSERT INTO t1(a,b,c,d) SELECT a, 2, c, 10-d FROM t1;
CREATE INDEX t1b ON t1(b);
}
do_catchsql_test aggorderby-1.2 {
SELECT b, group_concat(a ORDER BY max(d)) FROM t1 GROUP BY b;
} {1 {misuse of aggregate function max()}}
do_catchsql_test aggorderby-1.3 {
SELECT abs(a ORDER BY max(d)) FROM t1;
} {1 {ORDER BY may not be used with non-aggregate abs()}}
do_execsql_test aggorderby-2.0 {
SELECT group_concat(a ORDER BY a) FROM t1 WHERE b=1;
} {0,1,2,3,4,5,6,7,8,9}
do_execsql_test aggorderby-2.1 {
SELECT group_concat(a ORDER BY c) FROM t1 WHERE b=1;
} {0,7,4,1,8,5,2,9,6,3}
do_execsql_test aggorderby-2.2 {
SELECT group_concat(a ORDER BY b, d) FROM t1;
} {3,6,9,2,5,8,1,4,7,0,0,7,4,1,8,5,2,9,6,3}
do_execsql_test aggorderby-2.3 {
SELECT string_agg(a, ',' ORDER BY b DESC, d) FROM t1;
} {0,7,4,1,8,5,2,9,6,3,3,6,9,2,5,8,1,4,7,0}
do_execsql_test aggorderby-2.4 {
SELECT b, group_concat(a ORDER BY d) FROM t1 GROUP BY b ORDER BY b;
} {1 3,6,9,2,5,8,1,4,7,0 2 0,7,4,1,8,5,2,9,6,3}
do_execsql_test aggorderby-3.0 {
SELECT group_concat(DISTINCT a ORDER BY a) FROM t1;
} {0,1,2,3,4,5,6,7,8,9}
do_execsql_test aggorderby-3.1 {
SELECT group_concat(DISTINCT a ORDER BY c) FROM t1;
} {0,7,4,1,8,5,2,9,6,3}
do_execsql_test aggorderby-4.0 {
SELECT count(ORDER BY a) FROM t1;
} 20
do_execsql_test aggorderby-4.1 {
SELECT c, max(a ORDER BY a) FROM t1;
} {7 9}
do_execsql_test aggorderby-5.0 {
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t3;
CREATE TABLE t1(a TEXT); INSERT INTO t1 VALUES('aaa'),('bbb');
CREATE TABLE t3(d TEXT); INSERT INTO t3 VALUES('/'),('-');
SELECT (SELECT string_agg(a,d) FROM t3) FROM t1;
} {aaa-aaa bbb-bbb}
do_execsql_test aggorderby-5.1 {
SELECT (SELECT group_concat(a,d ORDER BY d) FROM t3) FROM t1;
} {aaa/aaa bbb/bbb}
do_execsql_test aggorderby-5.2 {
SELECT (SELECT string_agg(a,d ORDER BY d DESC) FROM t3) FROM t1;
} {aaa-aaa bbb-bbb}
do_execsql_test aggorderby-5.3 {
SELECT (SELECT string_agg(a,'#' ORDER BY d) FROM t3) FROM t1;
} {aaa#aaa bbb#bbb}
finish_test