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

Improve documentation #344

Merged
merged 2 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,34 @@ using the ``when`` method and to set the default value using ``else_``.
SELECT "id",CASE WHEN "fname"='Tom' THEN 'It was Tom' WHEN "fname"='John' THEN 'It was John' ELSE 'It was someone else.' END "who_was_it" FROM "customers"


With Clause
"""""""""""""""

With clause allows give a sub-query block a name, which can be referenced in several places within the main SQL query.
The SQL WITH clause is basically a drop-in replacement to the normal sub-query.

.. code-block:: python

from pypika import Table, AliasedQuery, Query

customers = Table('customers')

sub_query = (Query
.from_(customers)
.select('*'))

test_query = (Query
.with_(sub_query, "an_alias")
.from_(AliasedQuery("an_alias"))
.select('*'))

You can use as much as `.with_()` as you want.

.. code-block:: sql

WITH an_alias AS (SELECT * FROM "customers") SELECT * FROM an_alias


Inserting Data
^^^^^^^^^^^^^^

Expand Down
17 changes: 17 additions & 0 deletions docs/3_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ The `ROLLUP` modifier allows for aggregating to higher levels that the given gro

SELECT "id","category",SUM("price") FROM "products" GROUP BY ROLLUP("id","category")

Pseudo Column
------------------

A pseudo-column is an SQL assigned value (pseudo-field) used in the same context as an column, but not stored on disk.
The pseudo-column can change from database to database, so here it's possible to define them.

.. code-block:: python

from pypika import Query, PseudoColumn

CurrentDate = PseudoColumn('current_date')

Query.from_('products').select(CurrentDate)

.. code-block:: sql

SELECT current_date FROM "products"

Analytic Queries
----------------
Expand Down