A small example of SQL to SQLAlchemy conversions _mssql

Source: Internet
Author: User

Copy Code code as follows:

Select To_char (date_published, ' yyyymm ') Yo from blog Group by Yo;

Convert to SQLAlchemy statement


You can use Extract ()

Copy Code code as follows:

IN[3]: Year_field = db.func.extract (' year ', articlemodel.date_published)

IN[4]: Month_field = db.func.extract (' month ', articlemodel.date_published)

IN[5]: Yonth_field = Year_field *100+ Month_field

IN[6]:p rint db.session.query (Yonth_field.label (' Yonth '), Db.func.count (1)). Group_by (' Yonth ')

SELECT EXTRACT article_meta.date_published *:p aram_1 + EXTRACT (month from article_meta.date_published) as Yonth, COUNT (:p aram_2) as Count_1
From Article_meta JOIN article on article_meta.id = Article.id GROUPBY yonth

You can also call a database-supported function or stored procedure directly via Func
Copy Code code as follows:

Yonth = Db.func.to_char (articlemodel.date_published, ' Yyyymm '). Label (' Yonth ')
Archive_list = (Db.session.query (Yonth,
Db.func.count (1)). Group_by (' Yonth '). All ())

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.