From d8dcc92683ac850fd34c029a3fb055aa017d4142 Mon Sep 17 00:00:00 2001 From: sufleR Date: Wed, 9 Dec 2015 23:40:07 +0100 Subject: [PATCH] add examples --- README.md | 6 ++++++ examples/README.md | 10 ++++++++++ examples/distinct_values.sql.erb | 14 ++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 examples/README.md create mode 100644 examples/distinct_values.sql.erb diff --git a/README.md b/README.md index ba3523f..282909c 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,12 @@ FROM players WHERE <%= partial :email_partial %> ``` +## Examples + +Check examples folder for some usefull queries. + +If you have some examples to share please make pull request. + ## Contributing 1. Fork it ( https://github.com/[my-github-username]/sql_query/fork ) diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..4340918 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,10 @@ +### File list: + +* __distinct_values.sql.erb__ - implements "loose indexscan" in postgresql. More info [https://wiki.postgresql.org/wiki/Loose_indexscan](https://wiki.postgresql.org/wiki/Loose_indexscan) + +``` +SqlQuery.new(:distinct_values, table_name: :players, column_name: :player_type).execute + +``` +### + diff --git a/examples/distinct_values.sql.erb b/examples/distinct_values.sql.erb new file mode 100644 index 0000000..84b5296 --- /dev/null +++ b/examples/distinct_values.sql.erb @@ -0,0 +1,14 @@ +WITH RECURSIVE t AS ( + SELECT MIN(<%= @column_name %>) AS <%= @column_name %> + FROM <%= @table_name %> + UNION ALL + SELECT ( + SELECT MIN(<%= @column_name %>) FROM <%= @table_name %> + WHERE <%= @column_name %> > t.<%= @column_name %>) + FROM t WHERE t.<%= @column_name %> IS NOT NULL +) +SELECT <%= @column_name %> FROM t WHERE <%= @column_name %> IS NOT NULL +<% if @with_nulls %> + UNION ALL + SELECT NULL WHERE EXISTS(SELECT 1 FROM <%= @table_name %> WHERE <%= @column_name %> IS NULL) +<% end %>