Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: [Docs] Add example for custom bucketing with CASE #41787

Merged
merged 3 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/reference/sql/functions/conditional.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ an error message would be returned, mentioning that *'foo'* is of data type *key
which does not match the expected data type *integer* (based on result *10*).
===============================

[[sql-functions-conditional-case-groupby-custom-buckets]]
[TIP]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the TIP header and promote this to a section (=====) - I think it's big enough to warrant a proper entry.

===============================
CASE can be used as a GROUP BY key in a query to facilitate custom bucketing
and assign descriptive names to those buckets. If for example the number of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If, for example, the number....

values for a key are too many or, simply, ranges of those values are more
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the number ..... is too large. values.... are too many

interesting than every single value, CASE can create custom buckets as in the
following example:

[source, sql]
SELECT count(*) AS count,
CASE WHEN NVL(languages, 0) = 0 THEN 'zero'
WHEN languages = 1 THEN 'one'
WHEN languages = 2 THEN 'bilingual'
WHEN languages = 3 THEN 'trilingual'
ELSE 'multilingual'
END as lang_skills
FROM employees
GROUP BY lang_skills
ORDER BY lang_skills;

With this query, we can create normal grouping buckets for values _0, 1, 2, 3_ with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this query, one can create

descriptive names, and every value _>= 4_ falls into the _multilingual_ bucket.
===============================

[[sql-functions-conditional-coalesce]]
==== `COALESCE`

Expand Down
4 changes: 4 additions & 0 deletions docs/reference/sql/language/syntax/commands/select.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ Multiple aggregates used:
include-tagged::{sql-specs}/docs/docs.csv-spec[groupByAndMultipleAggs]
----

[TIP]
If custom bucketing is required, it can be achieved with the use of `<<sql-functions-conditional-case, CASE>>`,
as shown <<sql-functions-conditional-case-groupby-custom-buckets, here>>.

[[sql-syntax-group-by-implicit]]
===== Implicit Grouping

Expand Down