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 CROSS JOIN t1 g2 CROSS JOIN t1 g3;
63
SUM(DISTINCT LENGTH(g1.name)) s1,
64
SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
65
SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
66
FROM t1 g1 CROSS JOIN t1 g2 CROSS JOIN t1 g3 GROUP BY LENGTH(SUBSTRING(g3.name, 5, 10));
68
# here we explicitly request summing through temporary table (so
69
# Item_sum_sum_distinct::copy_or_same() is called)
71
SELECT SQL_BUFFER_RESULT
72
SUM(DISTINCT LENGTH(name)) s1,
73
SUM(DISTINCT SUBSTRING(NAME, 1, 3)) s2,
74
SUM(DISTINCT LENGTH(SUBSTRING(name, 1, 4))) s3
78
SUM(DISTINCT LENGTH(g1.name)) s1,
79
SUM(DISTINCT SUBSTRING(g2.name, 1, 3)) s2,
80
SUM(DISTINCT LENGTH(SUBSTRING(g3.name, 1, 4))) s3
81
FROM t1 g1 CROSS JOIN t1 g2 CROSS JOIN t1 g3 GROUP BY LENGTH(SUBSTRING(g3.name, 5, 10));
83
# this test demonstrates that strings are automatically converted to numbers
87
UPDATE t1 SET name=CONCAT(name, @l:=@l+1);
89
SELECT SUM(DISTINCT RIGHT(name, 1)) FROM t1;
91
# this is a test case for ordinary t1
93
SELECT SUM(DISTINCT id) FROM t1;
94
SELECT SUM(DISTINCT id % 11) FROM t1;