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

Window keyword support #71

Open
chanmix51 opened this issue Dec 8, 2014 · 2 comments
Open

Window keyword support #71

chanmix51 opened this issue Dec 8, 2014 · 2 comments

Comments

@chanmix51
Copy link

It seems sql-formatter does not recognize the window SQL keyword (SQL2003, Postgres does support it). The following query

  select 
    slug, 
    lag(slug) over published_at_wdw as next_slug, 
    lead(slug) over published_at_wdw as prev_slug 
  from 
    pomm.news 
  window published_at_wdw as (order by published_at desc)

is output as

  select 
    slug, 
    lag(slug) over published_at_wdw as next_slug, 
    lead(slug) over published_at_wdw as prev_slug 
  from 
    pomm.news window published_at_wdw as (
      order by 
        published_at desc
    )

sql-formatter 1.2.17

@jdorn
Copy link
Owner

jdorn commented Dec 9, 2014

I'm not familiar with that keyword. Should "window" be formatted like
"join"?

On Mon, Dec 8, 2014, 13:54 Grégoire HUBERT [email protected] wrote:

It seems sql-formatter does not recognize the window SQL keyword
(SQL2003, Postgres does support it). The following query

select
slug,
lag(slug) over published_at_wdw as next_slug,
lead(slug) over published_at_wdw as prev_slug
from
pomm.news
window published_at_wdw as (order by published_at desc)

is output as

select
slug,
lag(slug) over published_at_wdw as next_slug,
lead(slug) over published_at_wdw as prev_slug
from
pomm.news window published_at_wdw as (
order by
published_at desc
)


Reply to this email directly or view it on GitHub
#71.

@chanmix51
Copy link
Author

"window" is a declaration the same level as "from". It declares a data window some functions in the select use to give related data between each other (aka window functions, see Postgres documentation)

Syntax can be:

select
  wdw_function(field) over wdw_name as column,
 …
from table1
  join table2 …
  other joins
window wdw_name as (partition by field1 order by field2 desc)
…

or inline:

select
  wdw_function(field) over (partition by field1 order by field2) as column,
…

A comprehensive guide of SQL window syntax can be found in Postgres documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants