select_distinct.out 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. --
  2. -- SELECT_DISTINCT
  3. --
  4. --
  5. -- awk '{print $3;}' onek.data | sort -n | uniq
  6. --
  7. SELECT DISTINCT two FROM tmp ORDER BY 1;
  8. two
  9. -----
  10. 0
  11. 1
  12. (2 rows)
  13. --
  14. -- awk '{print $5;}' onek.data | sort -n | uniq
  15. --
  16. SELECT DISTINCT ten FROM tmp ORDER BY 1;
  17. ten
  18. -----
  19. 0
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. (10 rows)
  30. --
  31. -- awk '{print $16;}' onek.data | sort -d | uniq
  32. --
  33. SELECT DISTINCT string4 FROM tmp ORDER BY 1;
  34. string4
  35. ---------
  36. AAAAxx
  37. HHHHxx
  38. OOOOxx
  39. VVVVxx
  40. (4 rows)
  41. --
  42. -- awk '{print $3,$16,$5;}' onek.data | sort -d | uniq |
  43. -- sort +0n -1 +1d -2 +2n -3
  44. --
  45. SELECT DISTINCT two, string4, ten
  46. FROM tmp
  47. ORDER BY two using <, string4 using <, ten using <;
  48. two | string4 | ten
  49. -----+---------+-----
  50. 0 | AAAAxx | 0
  51. 0 | AAAAxx | 2
  52. 0 | AAAAxx | 4
  53. 0 | AAAAxx | 6
  54. 0 | AAAAxx | 8
  55. 0 | HHHHxx | 0
  56. 0 | HHHHxx | 2
  57. 0 | HHHHxx | 4
  58. 0 | HHHHxx | 6
  59. 0 | HHHHxx | 8
  60. 0 | OOOOxx | 0
  61. 0 | OOOOxx | 2
  62. 0 | OOOOxx | 4
  63. 0 | OOOOxx | 6
  64. 0 | OOOOxx | 8
  65. 0 | VVVVxx | 0
  66. 0 | VVVVxx | 2
  67. 0 | VVVVxx | 4
  68. 0 | VVVVxx | 6
  69. 0 | VVVVxx | 8
  70. 1 | AAAAxx | 1
  71. 1 | AAAAxx | 3
  72. 1 | AAAAxx | 5
  73. 1 | AAAAxx | 7
  74. 1 | AAAAxx | 9
  75. 1 | HHHHxx | 1
  76. 1 | HHHHxx | 3
  77. 1 | HHHHxx | 5
  78. 1 | HHHHxx | 7
  79. 1 | HHHHxx | 9
  80. 1 | OOOOxx | 1
  81. 1 | OOOOxx | 3
  82. 1 | OOOOxx | 5
  83. 1 | OOOOxx | 7
  84. 1 | OOOOxx | 9
  85. 1 | VVVVxx | 1
  86. 1 | VVVVxx | 3
  87. 1 | VVVVxx | 5
  88. 1 | VVVVxx | 7
  89. 1 | VVVVxx | 9
  90. (40 rows)
  91. --
  92. -- awk '{print $2;}' person.data |
  93. -- awk '{if(NF!=1){print $2;}else{print;}}' - emp.data |
  94. -- awk '{if(NF!=1){print $2;}else{print;}}' - student.data |
  95. -- awk 'BEGIN{FS=" ";}{if(NF!=1){print $5;}else{print;}}' - stud_emp.data |
  96. -- sort -n -r | uniq
  97. --
  98. SELECT DISTINCT p.age FROM person* p ORDER BY age using >;
  99. age
  100. -----
  101. 98
  102. 88
  103. 78
  104. 68
  105. 60
  106. 58
  107. 50
  108. 48
  109. 40
  110. 38
  111. 34
  112. 30
  113. 28
  114. 25
  115. 24
  116. 23
  117. 20
  118. 19
  119. 18
  120. 8
  121. (20 rows)
  122. --
  123. -- Check mentioning same column more than once
  124. --
  125. EXPLAIN (VERBOSE, COSTS OFF)
  126. SELECT count(*) FROM
  127. (SELECT DISTINCT two, four, two FROM tenk1) ss;
  128. QUERY PLAN
  129. --------------------------------------------------------
  130. Aggregate
  131. Output: count(*)
  132. -> HashAggregate
  133. Output: tenk1.two, tenk1.four, tenk1.two
  134. Group Key: tenk1.two, tenk1.four, tenk1.two
  135. -> Seq Scan on public.tenk1
  136. Output: tenk1.two, tenk1.four, tenk1.two
  137. (7 rows)
  138. SELECT count(*) FROM
  139. (SELECT DISTINCT two, four, two FROM tenk1) ss;
  140. count
  141. -------
  142. 4
  143. (1 row)
  144. --
  145. -- Compare results between plans using sorting and plans using hash
  146. -- aggregation. Force spilling in both cases by setting work_mem low.
  147. --
  148. SET work_mem='64kB';
  149. -- Produce results with sorting.
  150. SET enable_hashagg=FALSE;
  151. SET jit_above_cost=0;
  152. EXPLAIN (costs off)
  153. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  154. QUERY PLAN
  155. ------------------------------------------------
  156. Unique
  157. -> Sort
  158. Sort Key: ((g % 1000))
  159. -> Function Scan on generate_series g
  160. (4 rows)
  161. CREATE TABLE distinct_group_1 AS
  162. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  163. SET jit_above_cost TO DEFAULT;
  164. CREATE TABLE distinct_group_2 AS
  165. SELECT DISTINCT (g%1000)::text FROM generate_series(0,9999) g;
  166. SET enable_hashagg=TRUE;
  167. -- Produce results with hash aggregation.
  168. SET enable_sort=FALSE;
  169. SET jit_above_cost=0;
  170. EXPLAIN (costs off)
  171. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  172. QUERY PLAN
  173. ------------------------------------------
  174. HashAggregate
  175. Group Key: (g % 1000)
  176. -> Function Scan on generate_series g
  177. (3 rows)
  178. CREATE TABLE distinct_hash_1 AS
  179. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  180. SET jit_above_cost TO DEFAULT;
  181. CREATE TABLE distinct_hash_2 AS
  182. SELECT DISTINCT (g%1000)::text FROM generate_series(0,9999) g;
  183. SET enable_sort=TRUE;
  184. SET work_mem TO DEFAULT;
  185. -- Compare results
  186. (SELECT * FROM distinct_hash_1 EXCEPT SELECT * FROM distinct_group_1)
  187. UNION ALL
  188. (SELECT * FROM distinct_group_1 EXCEPT SELECT * FROM distinct_hash_1);
  189. ?column?
  190. ----------
  191. (0 rows)
  192. (SELECT * FROM distinct_hash_1 EXCEPT SELECT * FROM distinct_group_1)
  193. UNION ALL
  194. (SELECT * FROM distinct_group_1 EXCEPT SELECT * FROM distinct_hash_1);
  195. ?column?
  196. ----------
  197. (0 rows)
  198. DROP TABLE distinct_hash_1;
  199. DROP TABLE distinct_hash_2;
  200. DROP TABLE distinct_group_1;
  201. DROP TABLE distinct_group_2;
  202. --
  203. -- Also, some tests of IS DISTINCT FROM, which doesn't quite deserve its
  204. -- very own regression file.
  205. --
  206. CREATE TEMP TABLE disttable (f1 integer);
  207. INSERT INTO DISTTABLE VALUES(1);
  208. INSERT INTO DISTTABLE VALUES(2);
  209. INSERT INTO DISTTABLE VALUES(3);
  210. INSERT INTO DISTTABLE VALUES(NULL);
  211. -- basic cases
  212. SELECT f1, f1 IS DISTINCT FROM 2 as "not 2" FROM disttable;
  213. f1 | not 2
  214. ----+-------
  215. 1 | t
  216. 2 | f
  217. 3 | t
  218. | t
  219. (4 rows)
  220. SELECT f1, f1 IS DISTINCT FROM NULL as "not null" FROM disttable;
  221. f1 | not null
  222. ----+----------
  223. 1 | t
  224. 2 | t
  225. 3 | t
  226. | f
  227. (4 rows)
  228. SELECT f1, f1 IS DISTINCT FROM f1 as "false" FROM disttable;
  229. f1 | false
  230. ----+-------
  231. 1 | f
  232. 2 | f
  233. 3 | f
  234. | f
  235. (4 rows)
  236. SELECT f1, f1 IS DISTINCT FROM f1+1 as "not null" FROM disttable;
  237. f1 | not null
  238. ----+----------
  239. 1 | t
  240. 2 | t
  241. 3 | t
  242. | f
  243. (4 rows)
  244. -- check that optimizer constant-folds it properly
  245. SELECT 1 IS DISTINCT FROM 2 as "yes";
  246. yes
  247. -----
  248. t
  249. (1 row)
  250. SELECT 2 IS DISTINCT FROM 2 as "no";
  251. no
  252. ----
  253. f
  254. (1 row)
  255. SELECT 2 IS DISTINCT FROM null as "yes";
  256. yes
  257. -----
  258. t
  259. (1 row)
  260. SELECT null IS DISTINCT FROM null as "no";
  261. no
  262. ----
  263. f
  264. (1 row)
  265. -- negated form
  266. SELECT 1 IS NOT DISTINCT FROM 2 as "no";
  267. no
  268. ----
  269. f
  270. (1 row)
  271. SELECT 2 IS NOT DISTINCT FROM 2 as "yes";
  272. yes
  273. -----
  274. t
  275. (1 row)
  276. SELECT 2 IS NOT DISTINCT FROM null as "no";
  277. no
  278. ----
  279. f
  280. (1 row)
  281. SELECT null IS NOT DISTINCT FROM null as "yes";
  282. yes
  283. -----
  284. t
  285. (1 row)