Comments (4)
Does test_named_ctes
have an example of what you're looking for?
I think you want something like
cte_1 = With(Model1.objects.filter(**filters))
cte_2 = cte_1.join(Model2, field1=cte_1.col.field2).filter(**filters2)
final_query = (
cte_2.join(Model3, field1=cte_2.col.field2)
.with_cte(cte_1)
.with_cte(cte_2)
.filter(field2=cte_1.col.field2)
)
from django-cte.
The test_named_ctes
function really helped. Thanks!
But if the cte
s are unrelated to each other, but related to only the final query, like so:
with cte_1 as ( select * from a ),
cte_2 as ( select * from b )
select
*
from
some_table t1
left join cte_1 as t2 on t2.x = t1.y
left join cte_2 as t3 on t3.x = t1.z
Then the above method does not work because we are allowed only one join
statement and subsequent filters
.
Even if I provide the join
statement with the arg. _join_type=LOUTER
, then the subsequent filter
clauses create an INNER JOIN
in the resultant query.
I have tried the following (but it fails with error missing FROM-clause entry for table ...
:
Model3.objects.filter(...).with_cte(cte_1).with_cte(cte_2).annotate(
# need the query to create left join here
aggr_cte1=Case(
When(user_id=cte1.col.user_id, then=cte1.col.count),
default=0,
output_field=IntegerField()
),
# need the query to create left join here
aggr_cte2=Case(
When(user_id=cte2.col.user_id, then=cte2.col.count),
default=0,
output_field=IntegerField()
)
)
The CTE
does not allow chaining multiple left joins to the final query. Any thoughts ?
from django-cte.
OK, I found a way to chain multiple left joins, like so (bold code):
cte_1.join( cte_2.join( Model3, user_id=cte_2.col.agent_id, _join_type=LOUTER ), user_id=cte_1.col.agent_id, _join_type=LOUTER ).annotate( aggr_1=cte_1.col.calls_count, aggr_2=cte_2.col.calls_count, ).with_cte( cte_1 ).with_cte( cte_2 )
Say, if I had 5 such cte
s, it would really obfuscate the code. Nesting joins works, but is there a more straight-forward way to do this?
from django-cte.
I don't know of an alternative. Do you have an example of syntax would you prefer? Pull requests are welcome.
from django-cte.
Related Issues (20)
- Improve documentation HOT 3
- is there a way to select from a cte without joining it with a model? HOT 5
- Usage in the context of time series HOT 1
- Combining with django-reversion HOT 2
- Can I count all children in Queryset annotate? HOT 7
- annotate() usage in recursion HOT 5
- Unneccessary outer join breaks query HOT 1
- With(empty_qs) throws Error HOT 1
- “no such column”/“column does not exist” errors in Django 4.2 HOT 9
- Not able to do full outer join with django_cte HOT 2
- hnej
- `.update()` does not work when accessing nested tables HOT 1
- Minor : typo in the doc
- How to use as default manager? HOT 1
- Prefetch objects with their nested children
- issue while trying to use with_cte on EmptyResultSet HOT 1
- issue while using cte with union querysets
- Django 4.2 error: each UNION query must have the same number of columns HOT 3
- it's wrong number when i wnat to get total number by using count()
- WITH query name specified more than once HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-cte.