select_distinct.sql 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. --
  9. -- awk '{print $5;}' onek.data | sort -n | uniq
  10. --
  11. SELECT DISTINCT ten FROM tmp ORDER BY 1;
  12. --
  13. -- awk '{print $16;}' onek.data | sort -d | uniq
  14. --
  15. SELECT DISTINCT string4 FROM tmp ORDER BY 1;
  16. --
  17. -- awk '{print $3,$16,$5;}' onek.data | sort -d | uniq |
  18. -- sort +0n -1 +1d -2 +2n -3
  19. --
  20. SELECT DISTINCT two, string4, ten
  21. FROM tmp
  22. ORDER BY two using <, string4 using <, ten using <;
  23. --
  24. -- awk '{print $2;}' person.data |
  25. -- awk '{if(NF!=1){print $2;}else{print;}}' - emp.data |
  26. -- awk '{if(NF!=1){print $2;}else{print;}}' - student.data |
  27. -- awk 'BEGIN{FS=" ";}{if(NF!=1){print $5;}else{print;}}' - stud_emp.data |
  28. -- sort -n -r | uniq
  29. --
  30. SELECT DISTINCT p.age FROM person* p ORDER BY age using >;
  31. --
  32. -- Check mentioning same column more than once
  33. --
  34. EXPLAIN (VERBOSE, COSTS OFF)
  35. SELECT count(*) FROM
  36. (SELECT DISTINCT two, four, two FROM tenk1) ss;
  37. SELECT count(*) FROM
  38. (SELECT DISTINCT two, four, two FROM tenk1) ss;
  39. --
  40. -- Compare results between plans using sorting and plans using hash
  41. -- aggregation. Force spilling in both cases by setting work_mem low.
  42. --
  43. SET work_mem='64kB';
  44. -- Produce results with sorting.
  45. SET enable_hashagg=FALSE;
  46. SET jit_above_cost=0;
  47. EXPLAIN (costs off)
  48. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  49. CREATE TABLE distinct_group_1 AS
  50. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  51. SET jit_above_cost TO DEFAULT;
  52. CREATE TABLE distinct_group_2 AS
  53. SELECT DISTINCT (g%1000)::text FROM generate_series(0,9999) g;
  54. SET enable_hashagg=TRUE;
  55. -- Produce results with hash aggregation.
  56. SET enable_sort=FALSE;
  57. SET jit_above_cost=0;
  58. EXPLAIN (costs off)
  59. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  60. CREATE TABLE distinct_hash_1 AS
  61. SELECT DISTINCT g%1000 FROM generate_series(0,9999) g;
  62. SET jit_above_cost TO DEFAULT;
  63. CREATE TABLE distinct_hash_2 AS
  64. SELECT DISTINCT (g%1000)::text FROM generate_series(0,9999) g;
  65. SET enable_sort=TRUE;
  66. SET work_mem TO DEFAULT;
  67. -- Compare results
  68. (SELECT * FROM distinct_hash_1 EXCEPT SELECT * FROM distinct_group_1)
  69. UNION ALL
  70. (SELECT * FROM distinct_group_1 EXCEPT SELECT * FROM distinct_hash_1);
  71. (SELECT * FROM distinct_hash_1 EXCEPT SELECT * FROM distinct_group_1)
  72. UNION ALL
  73. (SELECT * FROM distinct_group_1 EXCEPT SELECT * FROM distinct_hash_1);
  74. DROP TABLE distinct_hash_1;
  75. DROP TABLE distinct_hash_2;
  76. DROP TABLE distinct_group_1;
  77. DROP TABLE distinct_group_2;
  78. --
  79. -- Also, some tests of IS DISTINCT FROM, which doesn't quite deserve its
  80. -- very own regression file.
  81. --
  82. CREATE TEMP TABLE disttable (f1 integer);
  83. INSERT INTO DISTTABLE VALUES(1);
  84. INSERT INTO DISTTABLE VALUES(2);
  85. INSERT INTO DISTTABLE VALUES(3);
  86. INSERT INTO DISTTABLE VALUES(NULL);
  87. -- basic cases
  88. SELECT f1, f1 IS DISTINCT FROM 2 as "not 2" FROM disttable;
  89. SELECT f1, f1 IS DISTINCT FROM NULL as "not null" FROM disttable;
  90. SELECT f1, f1 IS DISTINCT FROM f1 as "false" FROM disttable;
  91. SELECT f1, f1 IS DISTINCT FROM f1+1 as "not null" FROM disttable;
  92. -- check that optimizer constant-folds it properly
  93. SELECT 1 IS DISTINCT FROM 2 as "yes";
  94. SELECT 2 IS DISTINCT FROM 2 as "no";
  95. SELECT 2 IS DISTINCT FROM null as "yes";
  96. SELECT null IS DISTINCT FROM null as "no";
  97. -- negated form
  98. SELECT 1 IS NOT DISTINCT FROM 2 as "no";
  99. SELECT 2 IS NOT DISTINCT FROM 2 as "yes";
  100. SELECT 2 IS NOT DISTINCT FROM null as "no";
  101. SELECT null IS NOT DISTINCT FROM null as "yes";