How to build Django Query Expression for Window functionCalendar tables in PostgreSQL 9How to flush output of print function?How to combine 2 or more querysets in a Django view?How to query as GROUP BY in django?How do I do a not equal in Django queryset filtering?How to debug in Django, the good way?How to write a sql query to sum total for each day?How to reset Django admin password?How to check Django versionTotal percentage of a series of positive and negative percentagesSQL - cumulative sum in postgres
Why does it seem the best way to make a living is to invest in real estate?
Bothered by watching coworkers slacking off
How deep is the liquid in a half-full hemisphere?
Avoiding dust scattering when you drill
How dangerous is a very out-of-true disc brake wheel?
Can a passenger predict that an airline or a tour operator is about to go bankrupt?
Is "weekend warrior" derogatory?
Is the "spacetime" the same thing as the mathematical 4th dimension?
Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?
Does the 'java' command compile Java programs?
Could Boris Johnson face criminal charges for illegally proroguing Parliament?
How to identify whether a publisher is genuine or not?
What's the global, general word that stands for "center tone of a song"?
Drawing Maps; flat distortion
Duck, duck, gone!
Why does `FindFit` fail so badly in this simple case?
Are there types of animals that can't make the trip to space? (physiologically)
If I travelled back in time to invest in X company to make a fortune, roughly what is the probability that it would fail?
What is the point of impeaching Trump?
Isn't the detector always measuring, and thus always collapsing the state?
Knights and Knaves: What does C say?
Wondering why they used ultrafast diodes in a 50 or 60Hz bridge?
Can I cast Death Ward on additional creatures without causing previous castings to end?
Giving a good fancy look to a simple table
How to build Django Query Expression for Window function
Calendar tables in PostgreSQL 9How to flush output of print function?How to combine 2 or more querysets in a Django view?How to query as GROUP BY in django?How do I do a not equal in Django queryset filtering?How to debug in Django, the good way?How to write a sql query to sum total for each day?How to reset Django admin password?How to check Django versionTotal percentage of a series of positive and negative percentagesSQL - cumulative sum in postgres
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I have postgres query, and I want to represent it using Django QuerySet builder
I have a table:
history_events
date -------------------------- amount
2019-03-16 16:03:11.49294+05 250.00
2019-03-18 14:56:30.224846+05 250.00
2019-03-18 15:07:30.579531+05 250.00
2019-03-18 20:52:53.581835+05 5.00
2019-03-18 22:33:21.598517+05 1000.00
2019-03-18 22:50:57.157465+05 1.00
2019-03-18 22:51:44.058534+05 2.00
2019-03-18 23:11:29.531447+05 255.00
2019-03-18 23:43:43.143171+05 250.00
2019-03-18 23:44:47.445534+05 500.00
2019-03-18 23:59:23.007685+05 250.00
2019-03-19 00:01:05.103574+05 255.00
2019-03-19 00:01:05.107682+05 250.00
2019-03-19 00:01:05.11454+05 500.00
2019-03-19 00:03:48.182851+05 255.00
and I need to build graphic using this data with step-by step incrementing amount sum by dates
This SQL collects correct data:
with data as (
select
date(date) as day,
sum(amount) as day_sum
from history_event
group by day
)
select
day,
day_sum,
sum(day_sum) over (order by day asc rows between unbounded preceding and current row)
from data
But I can not understand how to build correct Queryset expression for this
Another problem - there is no data for some days, and they do not appear on my graph
django python-3.x postgresql
add a comment
|
I have postgres query, and I want to represent it using Django QuerySet builder
I have a table:
history_events
date -------------------------- amount
2019-03-16 16:03:11.49294+05 250.00
2019-03-18 14:56:30.224846+05 250.00
2019-03-18 15:07:30.579531+05 250.00
2019-03-18 20:52:53.581835+05 5.00
2019-03-18 22:33:21.598517+05 1000.00
2019-03-18 22:50:57.157465+05 1.00
2019-03-18 22:51:44.058534+05 2.00
2019-03-18 23:11:29.531447+05 255.00
2019-03-18 23:43:43.143171+05 250.00
2019-03-18 23:44:47.445534+05 500.00
2019-03-18 23:59:23.007685+05 250.00
2019-03-19 00:01:05.103574+05 255.00
2019-03-19 00:01:05.107682+05 250.00
2019-03-19 00:01:05.11454+05 500.00
2019-03-19 00:03:48.182851+05 255.00
and I need to build graphic using this data with step-by step incrementing amount sum by dates
This SQL collects correct data:
with data as (
select
date(date) as day,
sum(amount) as day_sum
from history_event
group by day
)
select
day,
day_sum,
sum(day_sum) over (order by day asc rows between unbounded preceding and current row)
from data
But I can not understand how to build correct Queryset expression for this
Another problem - there is no data for some days, and they do not appear on my graph
django python-3.x postgresql
look at window functions docs
– Kyryl Havrylenko
Mar 28 at 16:37
Did the answer help?
– Endre Both
Apr 8 at 17:46
add a comment
|
I have postgres query, and I want to represent it using Django QuerySet builder
I have a table:
history_events
date -------------------------- amount
2019-03-16 16:03:11.49294+05 250.00
2019-03-18 14:56:30.224846+05 250.00
2019-03-18 15:07:30.579531+05 250.00
2019-03-18 20:52:53.581835+05 5.00
2019-03-18 22:33:21.598517+05 1000.00
2019-03-18 22:50:57.157465+05 1.00
2019-03-18 22:51:44.058534+05 2.00
2019-03-18 23:11:29.531447+05 255.00
2019-03-18 23:43:43.143171+05 250.00
2019-03-18 23:44:47.445534+05 500.00
2019-03-18 23:59:23.007685+05 250.00
2019-03-19 00:01:05.103574+05 255.00
2019-03-19 00:01:05.107682+05 250.00
2019-03-19 00:01:05.11454+05 500.00
2019-03-19 00:03:48.182851+05 255.00
and I need to build graphic using this data with step-by step incrementing amount sum by dates
This SQL collects correct data:
with data as (
select
date(date) as day,
sum(amount) as day_sum
from history_event
group by day
)
select
day,
day_sum,
sum(day_sum) over (order by day asc rows between unbounded preceding and current row)
from data
But I can not understand how to build correct Queryset expression for this
Another problem - there is no data for some days, and they do not appear on my graph
django python-3.x postgresql
I have postgres query, and I want to represent it using Django QuerySet builder
I have a table:
history_events
date -------------------------- amount
2019-03-16 16:03:11.49294+05 250.00
2019-03-18 14:56:30.224846+05 250.00
2019-03-18 15:07:30.579531+05 250.00
2019-03-18 20:52:53.581835+05 5.00
2019-03-18 22:33:21.598517+05 1000.00
2019-03-18 22:50:57.157465+05 1.00
2019-03-18 22:51:44.058534+05 2.00
2019-03-18 23:11:29.531447+05 255.00
2019-03-18 23:43:43.143171+05 250.00
2019-03-18 23:44:47.445534+05 500.00
2019-03-18 23:59:23.007685+05 250.00
2019-03-19 00:01:05.103574+05 255.00
2019-03-19 00:01:05.107682+05 250.00
2019-03-19 00:01:05.11454+05 500.00
2019-03-19 00:03:48.182851+05 255.00
and I need to build graphic using this data with step-by step incrementing amount sum by dates
This SQL collects correct data:
with data as (
select
date(date) as day,
sum(amount) as day_sum
from history_event
group by day
)
select
day,
day_sum,
sum(day_sum) over (order by day asc rows between unbounded preceding and current row)
from data
But I can not understand how to build correct Queryset expression for this
Another problem - there is no data for some days, and they do not appear on my graph
django python-3.x postgresql
django python-3.x postgresql
asked Mar 28 at 16:02
Ilya IbulaevIlya Ibulaev
204 bronze badges
204 bronze badges
look at window functions docs
– Kyryl Havrylenko
Mar 28 at 16:37
Did the answer help?
– Endre Both
Apr 8 at 17:46
add a comment
|
look at window functions docs
– Kyryl Havrylenko
Mar 28 at 16:37
Did the answer help?
– Endre Both
Apr 8 at 17:46
look at window functions docs
– Kyryl Havrylenko
Mar 28 at 16:37
look at window functions docs
– Kyryl Havrylenko
Mar 28 at 16:37
Did the answer help?
– Endre Both
Apr 8 at 17:46
Did the answer help?
– Endre Both
Apr 8 at 17:46
add a comment
|
1 Answer
1
active
oldest
votes
Nested queries like yours cannot be easily defined in ORM syntax. Subquery is limited to correlated subqueries returning a single value. This often results in contorted and inefficient ORM workarounds for queries that you can easily express in SQL.
In this case, you can use two Window functions combined with a distinct clause.
result = (Event.objects
.values('date', 'amount')
.annotate(day_sum=Window(
expression=Sum('amount'),
partition_by=[F('date')],
))
.annotate(total=Window(
expression=Sum('amount'),
frame=RowRange(start=None, end=0),
order_by=F('date').asc(),
))
.distinct('date')
.order_by('date', '-total')
)
You need to order by '-total'
as otherwise distinct
discards the wrong rows, leaving you with less than the correct amounts in total
.
As to the missing days; SQL has no inherent concept of calendars (and therefore of missing dates) and unless you have lots of data, it should be easier to add missing days in a Python loop. In SQL, you would do it with a calendar table.
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55402088%2fhow-to-build-django-query-expression-for-window-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Nested queries like yours cannot be easily defined in ORM syntax. Subquery is limited to correlated subqueries returning a single value. This often results in contorted and inefficient ORM workarounds for queries that you can easily express in SQL.
In this case, you can use two Window functions combined with a distinct clause.
result = (Event.objects
.values('date', 'amount')
.annotate(day_sum=Window(
expression=Sum('amount'),
partition_by=[F('date')],
))
.annotate(total=Window(
expression=Sum('amount'),
frame=RowRange(start=None, end=0),
order_by=F('date').asc(),
))
.distinct('date')
.order_by('date', '-total')
)
You need to order by '-total'
as otherwise distinct
discards the wrong rows, leaving you with less than the correct amounts in total
.
As to the missing days; SQL has no inherent concept of calendars (and therefore of missing dates) and unless you have lots of data, it should be easier to add missing days in a Python loop. In SQL, you would do it with a calendar table.
add a comment
|
Nested queries like yours cannot be easily defined in ORM syntax. Subquery is limited to correlated subqueries returning a single value. This often results in contorted and inefficient ORM workarounds for queries that you can easily express in SQL.
In this case, you can use two Window functions combined with a distinct clause.
result = (Event.objects
.values('date', 'amount')
.annotate(day_sum=Window(
expression=Sum('amount'),
partition_by=[F('date')],
))
.annotate(total=Window(
expression=Sum('amount'),
frame=RowRange(start=None, end=0),
order_by=F('date').asc(),
))
.distinct('date')
.order_by('date', '-total')
)
You need to order by '-total'
as otherwise distinct
discards the wrong rows, leaving you with less than the correct amounts in total
.
As to the missing days; SQL has no inherent concept of calendars (and therefore of missing dates) and unless you have lots of data, it should be easier to add missing days in a Python loop. In SQL, you would do it with a calendar table.
add a comment
|
Nested queries like yours cannot be easily defined in ORM syntax. Subquery is limited to correlated subqueries returning a single value. This often results in contorted and inefficient ORM workarounds for queries that you can easily express in SQL.
In this case, you can use two Window functions combined with a distinct clause.
result = (Event.objects
.values('date', 'amount')
.annotate(day_sum=Window(
expression=Sum('amount'),
partition_by=[F('date')],
))
.annotate(total=Window(
expression=Sum('amount'),
frame=RowRange(start=None, end=0),
order_by=F('date').asc(),
))
.distinct('date')
.order_by('date', '-total')
)
You need to order by '-total'
as otherwise distinct
discards the wrong rows, leaving you with less than the correct amounts in total
.
As to the missing days; SQL has no inherent concept of calendars (and therefore of missing dates) and unless you have lots of data, it should be easier to add missing days in a Python loop. In SQL, you would do it with a calendar table.
Nested queries like yours cannot be easily defined in ORM syntax. Subquery is limited to correlated subqueries returning a single value. This often results in contorted and inefficient ORM workarounds for queries that you can easily express in SQL.
In this case, you can use two Window functions combined with a distinct clause.
result = (Event.objects
.values('date', 'amount')
.annotate(day_sum=Window(
expression=Sum('amount'),
partition_by=[F('date')],
))
.annotate(total=Window(
expression=Sum('amount'),
frame=RowRange(start=None, end=0),
order_by=F('date').asc(),
))
.distinct('date')
.order_by('date', '-total')
)
You need to order by '-total'
as otherwise distinct
discards the wrong rows, leaving you with less than the correct amounts in total
.
As to the missing days; SQL has no inherent concept of calendars (and therefore of missing dates) and unless you have lots of data, it should be easier to add missing days in a Python loop. In SQL, you would do it with a calendar table.
answered Mar 28 at 20:54
Endre BothEndre Both
3,3661 gold badge14 silver badges23 bronze badges
3,3661 gold badge14 silver badges23 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55402088%2fhow-to-build-django-query-expression-for-window-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
look at window functions docs
– Kyryl Havrylenko
Mar 28 at 16:37
Did the answer help?
– Endre Both
Apr 8 at 17:46