Created
March 21, 2019 05:57
-
-
Save leafsummer/ee90bfbc2854b361df9ee2f43f5c6cdb to your computer and use it in GitHub Desktop.
[peewee subquery in from clause]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# coding:utf-8 | |
from peewee import SQL | |
from peewee import QueryCompiler | |
from models import Blog | |
compiler = QueryCompiler('"', '?', {}, {}) | |
def from_subquery(): | |
inner = (Blog | |
.select(fn.COUNT(Blog.id).alias('blog_ct')) | |
.group_by(Blog.user)) | |
blog_ct = SQL('blog_ct') | |
outer = (Blog | |
.select(blog_ct, fn.COUNT(blog_ct).alias('blog_ct_n')) | |
.from_(inner) | |
.group_by(blog_ct)) | |
sql, params = compiler.generate_select(outer) | |
print sql | |
for q in outer: | |
print q.blog_ct, q.blog_ct_n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment