Git Product home page Git Product logo

python-sql-format's Introduction

python-sql-format

Demand : Input a sql string (not formatted), format it and print it out, get the formatted string.

So reference the method of https://github.com/sethelliott/sql_format, but not all like it.

Function

sql_format(sql, wrap_add=None, mode='none') in sql_format.py/sql_format

Result

Input ( test_sql.sql ):

select field_1, field_2, cast(date_format(case when length(sale_date) > 10 then substring(sale_date,
                                                     1,
                                                     10) else sale_date end, 'u') as int),
                                                     SUM(field_3)  as field_3, max(field_2)
from my_table
left join (Select field_2, sum(field_1) From (Select field_2, field_1 From another_table) as b group by field_2) as sub_table_on on my_table.field_2 = sub_table.field_2
left join table_three on my_table.field_1 = table_three.field_1
group by field_1, field_2 order by field_1 desc

Output ( test_format_sql.sql ):

SELECT
    field_1,
    field_2,
    CAST(DATE_FORMAT(CASE WHEN LENGTH(sale_date) > 10 THEN SUBSTRING(sale_date,
                            1,
                            10) ELSE sale_date END,
                    'u') AS int),
    SUM(field_3)  AS field_3,
    MAX(field_2)
FROM
    my_table
LEFT JOIN
    (
        SELECT
            field_2,
            SUM(field_1)
        FROM
            (
                SELECT
                    field_2,
                    field_1
                FROM
                    another_table) AS b
        GROUP BY
            field_2) AS sub_table_on
ON
    my_table.field_2 = sub_table.field_2
LEFT JOIN
    table_three
ON
    my_table.field_1 = table_three.field_1
GROUP BY
    field_1,
    field_2
ORDER BY
    field_1 DESC

python-sql-format's People

Contributors

longgb246 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.