Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sufleR committed Dec 9, 2015
1 parent 253e61b commit d8dcc92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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
```
###

14 changes: 14 additions & 0 deletions examples/distinct_values.sql.erb
Original file line number Diff line number Diff line change
@@ -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 %>

0 comments on commit d8dcc92

Please sign in to comment.