select_distinct_on.out 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. --
  2. -- SELECT_DISTINCT_ON
  3. --
  4. SELECT DISTINCT ON (string4) string4, two, ten
  5. FROM tmp
  6. ORDER BY string4 using <, two using >, ten using <;
  7. string4 | two | ten
  8. ---------+-----+-----
  9. AAAAxx | 1 | 1
  10. HHHHxx | 1 | 1
  11. OOOOxx | 1 | 1
  12. VVVVxx | 1 | 1
  13. (4 rows)
  14. -- this will fail due to conflict of ordering requirements
  15. SELECT DISTINCT ON (string4, ten) string4, two, ten
  16. FROM tmp
  17. ORDER BY string4 using <, two using <, ten using <;
  18. ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
  19. LINE 1: SELECT DISTINCT ON (string4, ten) string4, two, ten
  20. ^
  21. SELECT DISTINCT ON (string4, ten) string4, ten, two
  22. FROM tmp
  23. ORDER BY string4 using <, ten using >, two using <;
  24. string4 | ten | two
  25. ---------+-----+-----
  26. AAAAxx | 9 | 1
  27. AAAAxx | 8 | 0
  28. AAAAxx | 7 | 1
  29. AAAAxx | 6 | 0
  30. AAAAxx | 5 | 1
  31. AAAAxx | 4 | 0
  32. AAAAxx | 3 | 1
  33. AAAAxx | 2 | 0
  34. AAAAxx | 1 | 1
  35. AAAAxx | 0 | 0
  36. HHHHxx | 9 | 1
  37. HHHHxx | 8 | 0
  38. HHHHxx | 7 | 1
  39. HHHHxx | 6 | 0
  40. HHHHxx | 5 | 1
  41. HHHHxx | 4 | 0
  42. HHHHxx | 3 | 1
  43. HHHHxx | 2 | 0
  44. HHHHxx | 1 | 1
  45. HHHHxx | 0 | 0
  46. OOOOxx | 9 | 1
  47. OOOOxx | 8 | 0
  48. OOOOxx | 7 | 1
  49. OOOOxx | 6 | 0
  50. OOOOxx | 5 | 1
  51. OOOOxx | 4 | 0
  52. OOOOxx | 3 | 1
  53. OOOOxx | 2 | 0
  54. OOOOxx | 1 | 1
  55. OOOOxx | 0 | 0
  56. VVVVxx | 9 | 1
  57. VVVVxx | 8 | 0
  58. VVVVxx | 7 | 1
  59. VVVVxx | 6 | 0
  60. VVVVxx | 5 | 1
  61. VVVVxx | 4 | 0
  62. VVVVxx | 3 | 1
  63. VVVVxx | 2 | 0
  64. VVVVxx | 1 | 1
  65. VVVVxx | 0 | 0
  66. (40 rows)
  67. -- bug #5049: early 8.4.x chokes on volatile DISTINCT ON clauses
  68. select distinct on (1) floor(random()) as r, f1 from int4_tbl order by 1,2;
  69. r | f1
  70. ---+-------------
  71. 0 | -2147483647
  72. (1 row)