2
# Various tests for SUM(DISTINCT ...)
5
DROP TABLE IF EXISTS t1, t2;
9
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
14
# According to ANSI SQL, SUM(DISTINCT ...) should return NULL for empty
17
SELECT SUM(DISTINCT LENGTH(name)) s1 FROM t1;
19
# According to ANSI SQL, SUM(DISTINCT ...) should return NULL for records sets
20
# entirely consisting of NULLs
22
INSERT INTO t1 (gender, name) VALUES (NULL, NULL);
23
INSERT INTO t1 (gender, name) VALUES (NULL, NULL);
24
INSERT INTO t1 (gender, name) VALUES (NULL, NULL);
26
SELECT SUM(DISTINCT LENGTH(name)) s1 FROM t1;
29
# Filling table with t1
31
INSERT INTO t1 (gender, name) VALUES ('F', 'Helen'), ('F', 'Anastasia'),
32
('F', 'Katherine'), ('F', 'Margo'), ('F', 'Magdalene'), ('F', 'Mary');
34
CREATE TABLE t2 SELECT name FROM t1;
36
SELECT (SELECT SUM(DISTINCT LENGTH(name)) FROM t1) FROM t2;
40
INSERT INTO t1 (gender, name) VALUES ('F', 'Eva'), ('F', 'Sofia'),
41
('F', 'Sara'), ('F', 'Golda'), ('F', 'Toba'), ('F', 'Victory'),
42
('F', 'Faina'), ('F', 'Miriam'), ('F', 'Beki'), ('F', 'America'),
43
('F', 'Susan'), ('F', 'Glory'), ('F', 'Priscilla'), ('F', 'Rosmary'),
44
('F', 'Rose'), ('F', 'Margareth'), ('F', 'Elizabeth'), ('F', 'Meredith'),
45
('F', 'Julie'), ('F', 'Xenia'), ('F', 'Zena'), ('F', 'Olga'),
46
('F', 'Brunhilda'), ('F', 'Nataly'), ('F', 'Lara'), ('F', 'Svetlana'),
47
('F', 'Grethem'), ('F', 'Irene');
50
SUM(DISTINCT LENGTH(name)) s1,
51
SUM(DISTINCT SUBSTRING(NAME, 1, 3)) s2,
52
SUM(DISTINCT LENGTH(SUBSTRING(name, 1, 4))) s3
56
SUM(DISTINCT LENGTH(g1.name)) s1,
57
SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
58
SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
59
FROM t1 g1, t1 g2, t1 g3;
62
SUM(DISTINCT LENGTH(g1.name)) s1,
63
SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
64
SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
65
FROM t1 g1, t1 g2, t1 g3 GROUP BY LENGTH(SUBSTRING(g3.name, 5, 10));
67
# here we explicitly request summing through temporary table (so
68
# Item_sum_sum_distinct::copy_or_same() is called)
70
SELECT SQL_BUFFER_RESULT
71
SUM(DISTINCT LENGTH(name)) s1,
72
SUM(DISTINCT SUBSTRING(NAME, 1, 3)) s2,
73
SUM(DISTINCT LENGTH(SUBSTRING(name, 1, 4))) s3
76
SELECT SQL_BUFFER_RESULT
77
SUM(DISTINCT LENGTH(g1.name)) s1,
78
SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
79
SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
80
FROM t1 g1, t1 g2, t1 g3 GROUP BY LENGTH(SUBSTRING(g3.name, 5, 10));
82
# this test demonstrates that strings are automatically converted to numbers
86
UPDATE t1 SET name=CONCAT(name, @l:=@l+1);
88
SELECT SUM(DISTINCT RIGHT(name, 1)) FROM t1;
90
# this is a test case for ordinary t1
92
SELECT SUM(DISTINCT id) FROM t1;
93
SELECT SUM(DISTINCT id % 11) FROM t1;