Opening and Balance amount query for different account in postgresql










-1














I'm trying to set opening and Balance column in postgresql query where i input specific date range and first opening data should be upto starting date and Balance should be (opening data + Debit - Credit) in each date wise row.



*** Here is my sample database name "opening_and_closing"



date1 acc_no debit credit
01/01/2017 a 500 0
02/01/2017 a 0 400
03/01/2017 a 100 0
04/01/2017 a 800 0
05/01/2017 a 0 700
06/01/2017 a 800 0
01/01/2017 b 500 0
02/01/2017 b 0 400
03/01/2017 b 100 0
04/01/2017 b 800 0
05/01/2017 b 0 700
06/01/2017 b 800 0


* My expected query in postgresql
*
date range is 03/01/2017 to 06/01/2017



 opening : 100
date1 acc_no debit credit balance
03/01/2017 a 100 0 200
04/01/2017 a 800 0 1000
05/01/2017 a 0 700 300
06/01/2017 a 800 0 1100
opening : 100
date1 acc_no debit credit balance
03/01/2017 b 100 0 200
04/01/2017 b 800 0 1000
05/01/2017 b 0 700 300
06/01/2017 b 800 0 1100









share|improve this question



















  • 5




    How much are you offering to pay for someone to write this query for you?
    – Caius Jard
    Nov 12 '18 at 6:47










  • and there is an up vote
    – guradio
    Nov 12 '18 at 6:47










  • Hi i see your profile.. And you never accepted answer and rarely put up your query into your question.. Please.. Do a favor for them like accepted the answer.. And we are here not do free service code for you..
    – dwir182
    Nov 12 '18 at 6:52










  • #Caius Jard #guradil #dwir182, sorry dear i am a learner and unable to pay...
    – Zakir Hossain
    Nov 12 '18 at 8:57










  • The Debit and Credit columns seem to be reversed in your example input.
    – Mahesh H Viraktamath
    Nov 12 '18 at 9:13















-1














I'm trying to set opening and Balance column in postgresql query where i input specific date range and first opening data should be upto starting date and Balance should be (opening data + Debit - Credit) in each date wise row.



*** Here is my sample database name "opening_and_closing"



date1 acc_no debit credit
01/01/2017 a 500 0
02/01/2017 a 0 400
03/01/2017 a 100 0
04/01/2017 a 800 0
05/01/2017 a 0 700
06/01/2017 a 800 0
01/01/2017 b 500 0
02/01/2017 b 0 400
03/01/2017 b 100 0
04/01/2017 b 800 0
05/01/2017 b 0 700
06/01/2017 b 800 0


* My expected query in postgresql
*
date range is 03/01/2017 to 06/01/2017



 opening : 100
date1 acc_no debit credit balance
03/01/2017 a 100 0 200
04/01/2017 a 800 0 1000
05/01/2017 a 0 700 300
06/01/2017 a 800 0 1100
opening : 100
date1 acc_no debit credit balance
03/01/2017 b 100 0 200
04/01/2017 b 800 0 1000
05/01/2017 b 0 700 300
06/01/2017 b 800 0 1100









share|improve this question



















  • 5




    How much are you offering to pay for someone to write this query for you?
    – Caius Jard
    Nov 12 '18 at 6:47










  • and there is an up vote
    – guradio
    Nov 12 '18 at 6:47










  • Hi i see your profile.. And you never accepted answer and rarely put up your query into your question.. Please.. Do a favor for them like accepted the answer.. And we are here not do free service code for you..
    – dwir182
    Nov 12 '18 at 6:52










  • #Caius Jard #guradil #dwir182, sorry dear i am a learner and unable to pay...
    – Zakir Hossain
    Nov 12 '18 at 8:57










  • The Debit and Credit columns seem to be reversed in your example input.
    – Mahesh H Viraktamath
    Nov 12 '18 at 9:13













-1












-1








-1







I'm trying to set opening and Balance column in postgresql query where i input specific date range and first opening data should be upto starting date and Balance should be (opening data + Debit - Credit) in each date wise row.



*** Here is my sample database name "opening_and_closing"



date1 acc_no debit credit
01/01/2017 a 500 0
02/01/2017 a 0 400
03/01/2017 a 100 0
04/01/2017 a 800 0
05/01/2017 a 0 700
06/01/2017 a 800 0
01/01/2017 b 500 0
02/01/2017 b 0 400
03/01/2017 b 100 0
04/01/2017 b 800 0
05/01/2017 b 0 700
06/01/2017 b 800 0


* My expected query in postgresql
*
date range is 03/01/2017 to 06/01/2017



 opening : 100
date1 acc_no debit credit balance
03/01/2017 a 100 0 200
04/01/2017 a 800 0 1000
05/01/2017 a 0 700 300
06/01/2017 a 800 0 1100
opening : 100
date1 acc_no debit credit balance
03/01/2017 b 100 0 200
04/01/2017 b 800 0 1000
05/01/2017 b 0 700 300
06/01/2017 b 800 0 1100









share|improve this question















I'm trying to set opening and Balance column in postgresql query where i input specific date range and first opening data should be upto starting date and Balance should be (opening data + Debit - Credit) in each date wise row.



*** Here is my sample database name "opening_and_closing"



date1 acc_no debit credit
01/01/2017 a 500 0
02/01/2017 a 0 400
03/01/2017 a 100 0
04/01/2017 a 800 0
05/01/2017 a 0 700
06/01/2017 a 800 0
01/01/2017 b 500 0
02/01/2017 b 0 400
03/01/2017 b 100 0
04/01/2017 b 800 0
05/01/2017 b 0 700
06/01/2017 b 800 0


* My expected query in postgresql
*
date range is 03/01/2017 to 06/01/2017



 opening : 100
date1 acc_no debit credit balance
03/01/2017 a 100 0 200
04/01/2017 a 800 0 1000
05/01/2017 a 0 700 300
06/01/2017 a 800 0 1100
opening : 100
date1 acc_no debit credit balance
03/01/2017 b 100 0 200
04/01/2017 b 800 0 1000
05/01/2017 b 0 700 300
06/01/2017 b 800 0 1100






sql postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 10:04







Zakir Hossain

















asked Nov 12 '18 at 6:27









Zakir HossainZakir Hossain

5511




5511







  • 5




    How much are you offering to pay for someone to write this query for you?
    – Caius Jard
    Nov 12 '18 at 6:47










  • and there is an up vote
    – guradio
    Nov 12 '18 at 6:47










  • Hi i see your profile.. And you never accepted answer and rarely put up your query into your question.. Please.. Do a favor for them like accepted the answer.. And we are here not do free service code for you..
    – dwir182
    Nov 12 '18 at 6:52










  • #Caius Jard #guradil #dwir182, sorry dear i am a learner and unable to pay...
    – Zakir Hossain
    Nov 12 '18 at 8:57










  • The Debit and Credit columns seem to be reversed in your example input.
    – Mahesh H Viraktamath
    Nov 12 '18 at 9:13












  • 5




    How much are you offering to pay for someone to write this query for you?
    – Caius Jard
    Nov 12 '18 at 6:47










  • and there is an up vote
    – guradio
    Nov 12 '18 at 6:47










  • Hi i see your profile.. And you never accepted answer and rarely put up your query into your question.. Please.. Do a favor for them like accepted the answer.. And we are here not do free service code for you..
    – dwir182
    Nov 12 '18 at 6:52










  • #Caius Jard #guradil #dwir182, sorry dear i am a learner and unable to pay...
    – Zakir Hossain
    Nov 12 '18 at 8:57










  • The Debit and Credit columns seem to be reversed in your example input.
    – Mahesh H Viraktamath
    Nov 12 '18 at 9:13







5




5




How much are you offering to pay for someone to write this query for you?
– Caius Jard
Nov 12 '18 at 6:47




How much are you offering to pay for someone to write this query for you?
– Caius Jard
Nov 12 '18 at 6:47












and there is an up vote
– guradio
Nov 12 '18 at 6:47




and there is an up vote
– guradio
Nov 12 '18 at 6:47












Hi i see your profile.. And you never accepted answer and rarely put up your query into your question.. Please.. Do a favor for them like accepted the answer.. And we are here not do free service code for you..
– dwir182
Nov 12 '18 at 6:52




Hi i see your profile.. And you never accepted answer and rarely put up your query into your question.. Please.. Do a favor for them like accepted the answer.. And we are here not do free service code for you..
– dwir182
Nov 12 '18 at 6:52












#Caius Jard #guradil #dwir182, sorry dear i am a learner and unable to pay...
– Zakir Hossain
Nov 12 '18 at 8:57




#Caius Jard #guradil #dwir182, sorry dear i am a learner and unable to pay...
– Zakir Hossain
Nov 12 '18 at 8:57












The Debit and Credit columns seem to be reversed in your example input.
– Mahesh H Viraktamath
Nov 12 '18 at 9:13




The Debit and Credit columns seem to be reversed in your example input.
– Mahesh H Viraktamath
Nov 12 '18 at 9:13












1 Answer
1






active

oldest

votes


















0














***Create a table name "opening_and_closing" in your database



create table opening_and_closing (date1 date, acc_no varchar(250), debit numeric,credit numeric);


***Insert Values in your "opening_and_closing" table



insert into opening_and_closing
values ('2017-01-01','a',500,0),
('2017-01-02','a',0,400),
('2017-01-03','a',100,0),
('2017-01-04','a',800,0),
('2017-01-05','a',0,700),
('2017-01-06','a',800,0),
('2017-01-01','b',500,0),
('2017-01-02','b',0,400),
('2017-01-03','b',100,0),
('2017-01-04','b',800,0),
('2017-01-05','b',0,700),
('2017-01-06','b',800,0);


***Now Execute the following query



select date1,acc_no,opening,debit,credit,
(opening+sum(debit-credit) OVER (partition by acc_no order by date1)) as balance
from

(select date1,acc_no,debit,credit,
(select sum(debit)-sum(credit) from opening_and_closing where date1 < '2017-01-03' and acc_no ='a') as opening

from opening_and_closing
where date1 between '2017-01-03' and '2017-01-06'
and acc_no ='a'
group by 1,2,3,4
order by date1)x





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/3.0/"u003ecc by-sa 3.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%2f53256895%2fopening-and-balance-amount-query-for-different-account-in-postgresql%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














    ***Create a table name "opening_and_closing" in your database



    create table opening_and_closing (date1 date, acc_no varchar(250), debit numeric,credit numeric);


    ***Insert Values in your "opening_and_closing" table



    insert into opening_and_closing
    values ('2017-01-01','a',500,0),
    ('2017-01-02','a',0,400),
    ('2017-01-03','a',100,0),
    ('2017-01-04','a',800,0),
    ('2017-01-05','a',0,700),
    ('2017-01-06','a',800,0),
    ('2017-01-01','b',500,0),
    ('2017-01-02','b',0,400),
    ('2017-01-03','b',100,0),
    ('2017-01-04','b',800,0),
    ('2017-01-05','b',0,700),
    ('2017-01-06','b',800,0);


    ***Now Execute the following query



    select date1,acc_no,opening,debit,credit,
    (opening+sum(debit-credit) OVER (partition by acc_no order by date1)) as balance
    from

    (select date1,acc_no,debit,credit,
    (select sum(debit)-sum(credit) from opening_and_closing where date1 < '2017-01-03' and acc_no ='a') as opening

    from opening_and_closing
    where date1 between '2017-01-03' and '2017-01-06'
    and acc_no ='a'
    group by 1,2,3,4
    order by date1)x





    share|improve this answer

























      0














      ***Create a table name "opening_and_closing" in your database



      create table opening_and_closing (date1 date, acc_no varchar(250), debit numeric,credit numeric);


      ***Insert Values in your "opening_and_closing" table



      insert into opening_and_closing
      values ('2017-01-01','a',500,0),
      ('2017-01-02','a',0,400),
      ('2017-01-03','a',100,0),
      ('2017-01-04','a',800,0),
      ('2017-01-05','a',0,700),
      ('2017-01-06','a',800,0),
      ('2017-01-01','b',500,0),
      ('2017-01-02','b',0,400),
      ('2017-01-03','b',100,0),
      ('2017-01-04','b',800,0),
      ('2017-01-05','b',0,700),
      ('2017-01-06','b',800,0);


      ***Now Execute the following query



      select date1,acc_no,opening,debit,credit,
      (opening+sum(debit-credit) OVER (partition by acc_no order by date1)) as balance
      from

      (select date1,acc_no,debit,credit,
      (select sum(debit)-sum(credit) from opening_and_closing where date1 < '2017-01-03' and acc_no ='a') as opening

      from opening_and_closing
      where date1 between '2017-01-03' and '2017-01-06'
      and acc_no ='a'
      group by 1,2,3,4
      order by date1)x





      share|improve this answer























        0












        0








        0






        ***Create a table name "opening_and_closing" in your database



        create table opening_and_closing (date1 date, acc_no varchar(250), debit numeric,credit numeric);


        ***Insert Values in your "opening_and_closing" table



        insert into opening_and_closing
        values ('2017-01-01','a',500,0),
        ('2017-01-02','a',0,400),
        ('2017-01-03','a',100,0),
        ('2017-01-04','a',800,0),
        ('2017-01-05','a',0,700),
        ('2017-01-06','a',800,0),
        ('2017-01-01','b',500,0),
        ('2017-01-02','b',0,400),
        ('2017-01-03','b',100,0),
        ('2017-01-04','b',800,0),
        ('2017-01-05','b',0,700),
        ('2017-01-06','b',800,0);


        ***Now Execute the following query



        select date1,acc_no,opening,debit,credit,
        (opening+sum(debit-credit) OVER (partition by acc_no order by date1)) as balance
        from

        (select date1,acc_no,debit,credit,
        (select sum(debit)-sum(credit) from opening_and_closing where date1 < '2017-01-03' and acc_no ='a') as opening

        from opening_and_closing
        where date1 between '2017-01-03' and '2017-01-06'
        and acc_no ='a'
        group by 1,2,3,4
        order by date1)x





        share|improve this answer












        ***Create a table name "opening_and_closing" in your database



        create table opening_and_closing (date1 date, acc_no varchar(250), debit numeric,credit numeric);


        ***Insert Values in your "opening_and_closing" table



        insert into opening_and_closing
        values ('2017-01-01','a',500,0),
        ('2017-01-02','a',0,400),
        ('2017-01-03','a',100,0),
        ('2017-01-04','a',800,0),
        ('2017-01-05','a',0,700),
        ('2017-01-06','a',800,0),
        ('2017-01-01','b',500,0),
        ('2017-01-02','b',0,400),
        ('2017-01-03','b',100,0),
        ('2017-01-04','b',800,0),
        ('2017-01-05','b',0,700),
        ('2017-01-06','b',800,0);


        ***Now Execute the following query



        select date1,acc_no,opening,debit,credit,
        (opening+sum(debit-credit) OVER (partition by acc_no order by date1)) as balance
        from

        (select date1,acc_no,debit,credit,
        (select sum(debit)-sum(credit) from opening_and_closing where date1 < '2017-01-03' and acc_no ='a') as opening

        from opening_and_closing
        where date1 between '2017-01-03' and '2017-01-06'
        and acc_no ='a'
        group by 1,2,3,4
        order by date1)x






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 10:15









        Zakir HossainZakir Hossain

        5511




        5511



























            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%2f53256895%2fopening-and-balance-amount-query-for-different-account-in-postgresql%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

            Kleinkühnau

            Makov (Slowakei)

            Peter Parker: The Spectacular Spider-Man #308