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;









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










share|improve this question
























  • look at window functions docs

    – Kyryl Havrylenko
    Mar 28 at 16:37











  • Did the answer help?

    – Endre Both
    Apr 8 at 17:46

















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










share|improve this question
























  • look at window functions docs

    – Kyryl Havrylenko
    Mar 28 at 16:37











  • Did the answer help?

    – Endre Both
    Apr 8 at 17:46













0












0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












1 Answer
1






active

oldest

votes


















0
















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.






share|improve this answer


























    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
    );



    );














    draft saved

    draft discarded
















    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









    0
















    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.






    share|improve this answer





























      0
















      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.






      share|improve this answer



























        0














        0










        0









        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        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

































            draft saved

            draft discarded















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript