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

[FORMATTING] Treat QUALIFY similar to GROUP BY #539

Closed
jgontarz opened this issue Dec 7, 2022 · 3 comments · Fixed by #543
Closed

[FORMATTING] Treat QUALIFY similar to GROUP BY #539

jgontarz opened this issue Dec 7, 2022 · 3 comments · Fixed by #543
Labels

Comments

@jgontarz
Copy link

jgontarz commented Dec 7, 2022

Input data

Which SQL and options did you provide as input?
Language = Snowflake
All other options default

select
      "ID",
      "Pay" / 100 AS "Pay",
      "Status",
      "OpTtl" / 100 AS "OpTtl",
      "TestAmt" / 100 AS "TestAmt",
      CONVERT_TIMEZONE('UTC', $Timezone, "WrittenAt"::TIMESTAMP_NTZ) AS "LocalWrittenDt",
      CONVERT_TIMEZONE('UTC', $Timezone, "CompleteAt"::TIMESTAMP_NTZ) AS "LocalCompleteDt",
      CONVERT_TIMEZONE('UTC', $Timezone, "UpdatedAt"::TIMESTAMP_NTZ) AS "LocalUpdatedDt",
      CONVERT_TIMEZONE('UTC', $Timezone, "MessageTime"::TIMESTAMP_NTZ) as "ESTMessageTime",
      "ExternalAccountId" as "CustomerAccountId",
      "AffiliateId",
      "SourceSystemId",
      "DomainId",
      "RetailTicketId",
      "Test"
    from
      "StagingMARA"."Contract"
    where
      "ESTMessageTime" < $Todate Qualify dense_rank() over (
        partition by
          "ID"
        order by
          "ESTMessageTime" desc
      ) = 1

Expected Output

select
      "ID",
      "Pay" / 100 AS "Pay",
      "Status",
      "OpTtl" / 100 AS "OpTtl",
      "TestAmt" / 100 AS "TestAmt",
      CONVERT_TIMEZONE('UTC', $Timezone, "WrittenAt"::TIMESTAMP_NTZ) AS "LocalWrittenDt",
      CONVERT_TIMEZONE('UTC', $Timezone, "CompleteAt"::TIMESTAMP_NTZ) AS "LocalCompleteDt",
      CONVERT_TIMEZONE('UTC', $Timezone, "UpdatedAt"::TIMESTAMP_NTZ) AS "LocalUpdatedDt",
      CONVERT_TIMEZONE('UTC', $Timezone, "MessageTime"::TIMESTAMP_NTZ) as "ESTMessageTime",
      "ExternalAccountId" as "CustomerAccountId",
      "AffiliateId",
      "SourceSystemId",
      "DomainId",
      "RetailTicketId",
      "Test"
    from
      "StagingMARA"."Contract"
    where
      "ESTMessageTime" < $Todate 
    qualify dense_rank() over (
        partition by
          "ID"
        order by
          "ESTMessageTime" desc
                              ) = 1

Actual Output

select
  "ID",
  "Pay" / 100 AS "Pay",
  "Status",
  "OpTtl" / 100 AS "OpTtl",
  "TestAmt" / 100 AS "TestAmt",
  CONVERT_TIMEZONE('UTC', $Timezone, "WrittenAt"::TIMESTAMP_NTZ) AS "LocalWrittenDt",
  CONVERT_TIMEZONE('UTC', $Timezone, "CompleteAt"::TIMESTAMP_NTZ) AS "LocalCompleteDt",
  CONVERT_TIMEZONE('UTC', $Timezone, "UpdatedAt"::TIMESTAMP_NTZ) AS "LocalUpdatedDt",
  CONVERT_TIMEZONE('UTC', $Timezone, "MessageTime"::TIMESTAMP_NTZ) as "ESTMessageTime",
  "ExternalAccountId" as "CustomerAccountId",
  "AffiliateId",
  "SourceSystemId",
  "DomainId",
  "RetailTicketId",
  "Test"
from
  "StagingMARA"."Contract"
where
  "ESTMessageTime" < $Todate Qualify dense_rank() over (
    partition by
      "ID"
    order by
      "ESTMessageTime" desc
  ) = 1

Usage

  • How are you calling / using the library?
    I am using this through Chrome - https://sql-formatter-org.github.io/sql-formatter/.

  • What SQL language(s) does this apply to?
    Snowflake is all I have tried.

  • Which SQL Formatter version are you using?
    v12.0.3

Including a link to the Snowflake documentation for Query Syntax and QUALIFY documentation.
https://docs.snowflake.com/en/sql-reference/constructs.html

https://docs.snowflake.com/en/sql-reference/constructs/qualify.html

@jgontarz jgontarz added the bug label Dec 7, 2022
@nene
Copy link
Collaborator

nene commented Dec 7, 2022

Thanks for reporting.

This should be a simple fix to make.

@nene
Copy link
Collaborator

nene commented Dec 15, 2022

This is now fixed in 12.0.4 release.

@jgontarz
Copy link
Author

Thanks. Works great!

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

Successfully merging a pull request may close this issue.

2 participants