README.aggregate_tracking 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. SQL Language Extension: Improved aggregate handling
  2. Syntax
  3. ========
  4. GROUP BY
  5. --------
  6. SELECT ... FROM .... [GROUP BY group_by_list]
  7. group_by_list : group_by_item [, group_by_list];
  8. group_by_item : column_name
  9. | ordinal
  10. | udf
  11. | group_by_function;
  12. group_by_function : numeric_value_function
  13. | string_value_function
  14. | case_expression
  15. ;
  16. numeric_value_function : EXTRACT '(' timestamp_part FROM value ')';
  17. string_value_function : SUBSTRING '(' value FROM pos_short_integer ')'
  18. | SUBSTRING '(' value FROM pos_short_integer FOR nonneg_short_integer ')'
  19. | KW_UPPER '(' value ')'
  20. ;
  21. (FB1.0 only allowed GROUP BY column_name and udf)
  22. Ordinal references to a n-th select-item from the select-list (same as ORDER BY).
  23. The group_by_item cannot reference to any aggregate-function (also not burried
  24. inside a expression) from the same context.
  25. HAVING
  26. --------
  27. The having clause only allows aggregate functions or expressions
  28. that are part of the GROUP BY clause. Previously it was allowed
  29. to use columns that were not part of the GROUP BY clause.
  30. ORDER BY
  31. --------
  32. When the context is an aggregate statement then the ORDER BY
  33. clause only allows valid expressions. That are aggregate
  34. functions or expression part of the GROUP BY clause.
  35. Previously it was allowed to use non-valid expressions.
  36. Aggregate functions inside sub-selects
  37. --------
  38. It is now possible to use a aggregate function or expression
  39. contained in the GROUP BY clause inside a sub-select.
  40. (See Examples A)
  41. Mixing aggregate functions from different contexts
  42. --------
  43. You can use aggregate functions from different contexts inside
  44. a expression.
  45. (See Example B)
  46. Sub-selects are supported inside a aggregate function
  47. --------
  48. Using a singleton select expression inside a aggregate function is
  49. supported.
  50. (See Example C)
  51. Nested aggregate functions
  52. --------
  53. Using a aggregate function inside a other aggregate function is
  54. possible if the aggregate function inside is from a lower context.
  55. (See Example C)
  56. Author:
  57. Arno Brinkman <[email protected]>
  58. N O T E S
  59. =========
  60. - Not all expressions are currently alowed inside the GROUP BY list.
  61. (concatenation for example)
  62. - ORDER BY clause only accepts valid expressions (this was not for FB1.0)
  63. - HAVING clause only accepts valid expressions (this was not for FB1.0)
  64. - Using ordinal 'copies' the expression from the select list as does the
  65. order by clause. This means when a ordinal references to a sub-select
  66. the sub-select is at least executed twice.
  67. Examples
  68. ========
  69. A)
  70. SELECT
  71. r.RDB$RELATION_NAME,
  72. MAX(r.RDB$FIELD_POSITION),
  73. (SELECT
  74. r2.RDB$FIELD_NAME
  75. FROM
  76. RDB$RELATION_FIELDS r2
  77. WHERE
  78. r2.RDB$RELATION_NAME = r.RDB$RELATION_NAME and
  79. r2.RDB$FIELD_POSITION = MAX(r.RDB$FIELD_POSITION))
  80. FROM
  81. RDB$RELATION_FIELDS r
  82. GROUP BY
  83. 1
  84. SELECT
  85. rf.RDB$RELATION_NAME AS "Relationname",
  86. (SELECT
  87. r.RDB$RELATION_ID
  88. FROM
  89. RDB$RELATIONS r
  90. WHERE
  91. r.RDB$RELATION_NAME = rf.RDB$RELATION_NAME) AS "ID",
  92. COUNT(*) AS "Fields"
  93. FROM
  94. RDB$RELATION_FIELDS rf
  95. GROUP BY
  96. rf.RDB$RELATION_NAME
  97. B)
  98. SELECT
  99. r.RDB$RELATION_NAME,
  100. MAX(i.RDB$STATISTICS) AS "Max1",
  101. (SELECT
  102. COUNT(*) || ' - ' || MAX(i.RDB$STATISTICS)
  103. FROM
  104. RDB$RELATION_FIELDS rf
  105. WHERE
  106. rf.RDB$RELATION_NAME = r.RDB$RELATION_NAME) AS "Max2"
  107. FROM
  108. RDB$RELATIONS r
  109. JOIN RDB$INDICES i on (i.RDB$RELATION_NAME = r.RDB$RELATION_NAME)
  110. GROUP BY
  111. r.RDB$RELATION_NAME
  112. HAVING
  113. MIN(i.RDB$STATISTICS) <> MAX(i.RDB$STATISTICS)
  114. Note! This query gives results in FB1.0, but they are WRONG!
  115. C)
  116. SELECT
  117. r.RDB$RELATION_NAME,
  118. SUM((SELECT
  119. COUNT(*)
  120. FROM
  121. RDB$RELATION_FIELDS rf
  122. WHERE
  123. rf.RDB$RELATION_NAME = r.RDB$RELATION_NAME))
  124. FROM
  125. RDB$RELATIONS r
  126. JOIN RDB$INDICES i on (i.RDB$RELATION_NAME = r.RDB$RELATION_NAME)
  127. GROUP BY
  128. r.RDB$RELATION_NAME