common-table-expression

Join on a CTE in SQLAlchemy

自作多情 提交于 2020-12-15 06:41:30
问题 I'm trying to formulate a SQLAlchemy query that uses a CTE to build a table-like structure of an input list of tuples, and JOIN it with one of my tables (backend DB is Postgres). Conceptually, it would look like: WITH to_compare AS ( SELECT * FROM ( VALUES (1, 'flimflam'), (2, 'fimblefamble'), (3, 'pigglywiggly'), (4, 'beepboop') -- repeat for a couple dozen or hundred rows ) AS t (field1, field2) ) SELECT b.field1, b.field2, b.field3 FROM my_model b JOIN to_compare c ON (c.field1 = b.field1)

Randomize part of select from CTE output

ぃ、小莉子 提交于 2020-08-09 09:13:07
问题 In this question @GordonLinoff provided a solution (recursive common table expression) to my initial question. This is a follow-up question. Initial question : How can I loop through registrations until a certain amount (sum) of AmountPersons was reached and if the next AmountPersons was too high to be invited check the AmountPersons of the next row to see if it would fit? Please check the initial question via the link above to get the full picture. New situation : First we have 20 available

Altering the order of a hierarchical result generated by a Recursive CTE?

强颜欢笑 提交于 2020-08-06 04:31:45
问题 I am using MySQL and I am wondering if it is possible to alter the order of the result generated by recursive CTE. My Table has these columns: |----------|----------|----------|----------| | ID | parentID | title | Sort | |----------|----------|----------|----------| | 1 | null | Maria | 1 | | 2 | 1 | John | 2 | | 3 | 2 | Maria | 3 | | 4 | 1 | Anthony | 1 | | 5 | 4 | XXX | 1 | | 6 | 4 | ... | 2 | | 7 | 2 | ... | 2 | | 8 | 2 | ... | 1 | | 9 | 1 | ... | 3 | I use the following query (We don't

Altering the order of a hierarchical result generated by a Recursive CTE?

孤街醉人 提交于 2020-08-06 04:30:27
问题 I am using MySQL and I am wondering if it is possible to alter the order of the result generated by recursive CTE. My Table has these columns: |----------|----------|----------|----------| | ID | parentID | title | Sort | |----------|----------|----------|----------| | 1 | null | Maria | 1 | | 2 | 1 | John | 2 | | 3 | 2 | Maria | 3 | | 4 | 1 | Anthony | 1 | | 5 | 4 | XXX | 1 | | 6 | 4 | ... | 2 | | 7 | 2 | ... | 2 | | 8 | 2 | ... | 1 | | 9 | 1 | ... | 3 | I use the following query (We don't

Altering the order of a hierarchical result generated by a Recursive CTE?

孤者浪人 提交于 2020-08-06 04:29:09
问题 I am using MySQL and I am wondering if it is possible to alter the order of the result generated by recursive CTE. My Table has these columns: |----------|----------|----------|----------| | ID | parentID | title | Sort | |----------|----------|----------|----------| | 1 | null | Maria | 1 | | 2 | 1 | John | 2 | | 3 | 2 | Maria | 3 | | 4 | 1 | Anthony | 1 | | 5 | 4 | XXX | 1 | | 6 | 4 | ... | 2 | | 7 | 2 | ... | 2 | | 8 | 2 | ... | 1 | | 9 | 1 | ... | 3 | I use the following query (We don't