Closing balance of the previous day as an Opening balance of todayHow to list the tables in a SQLite database file that was opened with ATTACH?SQL Query Solution - Day By Day BalanceAdd opening and closing balance columnsAuto calculating columns in a sql table based on prevoius row dataAdd closing balance columns as per transaction (income-expenses)SQL help calculating Opening and Closing balance columnsSQL Query for Opening Balancesql query for difference between current row and previous row based on datetimeHow should i tally my opening and closing balanceCalculate the current balance in Query to add/subtract based on the transaction type?

Is first Ubuntu user root?

What is the meaning of “these lederhosen are riding up my Bundesliga”?

Joining lists with same elements

Given current technology, could TV display screens double as video camera sensors?

If I said I had $100 when asked, but I actually had $200, would I be lying by omission?

Reusing studs to hang shoe bins

Thought experiment and possible contradiction between electromagnetism and special relativity

To what extent should we fear giving offense?

Weighted smooth histogram

Did Dr. Hannibal Lecter like Clarice or was he attracted to her?

How were medieval castles built in swamps or marshes without draining them?

What are the IPSE’s, the ASPE’s, the FRIPSE’s and the GRIPSE’s?

Multiple delayed triggers from Massacre Girl interaction

Is it legal for source code containing undefined behavior to crash the compiler?

Where does learning new skills fit into Agile?

How does the OS tell whether an "Address is already in use"?

Why does the `ls` command sort files like this?

What stops you from using fixed income in developing countries?

What's special ammo in Destiny 2?

Retroactively modifying humans for Earth?

What does rm -rf $(!!) mean?

Why is the winner of Pow different everytime?

Is the Amazon rainforest the "world's lungs"?

3D cryptic featuring Mao, Stalin and Simba's uncle



Closing balance of the previous day as an Opening balance of today


How to list the tables in a SQLite database file that was opened with ATTACH?SQL Query Solution - Day By Day BalanceAdd opening and closing balance columnsAuto calculating columns in a sql table based on prevoius row dataAdd closing balance columns as per transaction (income-expenses)SQL help calculating Opening and Closing balance columnsSQL Query for Opening Balancesql query for difference between current row and previous row based on datetimeHow should i tally my opening and closing balanceCalculate the current balance in Query to add/subtract based on the transaction type?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I am developing a database application for a small electronics business. I need a SQL query which takes the closing balance of previous day as an opening balance of current day. I have following data tables



Expensis



ExpenseID Date Expense 

1 2019-03-01 2,000
2 2019-03-02 1,000
3 2019-03-03 500


Income



IncomeID Date Income

1 2019-03-01 10,000
2 2019-03-02 13,000
3 2019-03-03 10,000


Required result



Date Opening Balance Income Expense Closing Balance

2019-03-01 0 10,000 2,000 8,000
2019-03-02 8,000 13,000 1,000 20,000
2019-03-03 20,000 10,000 5,00 29,500









share|improve this question
































    2















    I am developing a database application for a small electronics business. I need a SQL query which takes the closing balance of previous day as an opening balance of current day. I have following data tables



    Expensis



    ExpenseID Date Expense 

    1 2019-03-01 2,000
    2 2019-03-02 1,000
    3 2019-03-03 500


    Income



    IncomeID Date Income

    1 2019-03-01 10,000
    2 2019-03-02 13,000
    3 2019-03-03 10,000


    Required result



    Date Opening Balance Income Expense Closing Balance

    2019-03-01 0 10,000 2,000 8,000
    2019-03-02 8,000 13,000 1,000 20,000
    2019-03-03 20,000 10,000 5,00 29,500









    share|improve this question




























      2












      2








      2








      I am developing a database application for a small electronics business. I need a SQL query which takes the closing balance of previous day as an opening balance of current day. I have following data tables



      Expensis



      ExpenseID Date Expense 

      1 2019-03-01 2,000
      2 2019-03-02 1,000
      3 2019-03-03 500


      Income



      IncomeID Date Income

      1 2019-03-01 10,000
      2 2019-03-02 13,000
      3 2019-03-03 10,000


      Required result



      Date Opening Balance Income Expense Closing Balance

      2019-03-01 0 10,000 2,000 8,000
      2019-03-02 8,000 13,000 1,000 20,000
      2019-03-03 20,000 10,000 5,00 29,500









      share|improve this question
















      I am developing a database application for a small electronics business. I need a SQL query which takes the closing balance of previous day as an opening balance of current day. I have following data tables



      Expensis



      ExpenseID Date Expense 

      1 2019-03-01 2,000
      2 2019-03-02 1,000
      3 2019-03-03 500


      Income



      IncomeID Date Income

      1 2019-03-01 10,000
      2 2019-03-02 13,000
      3 2019-03-03 10,000


      Required result



      Date Opening Balance Income Expense Closing Balance

      2019-03-01 0 10,000 2,000 8,000
      2019-03-02 8,000 13,000 1,000 20,000
      2019-03-03 20,000 10,000 5,00 29,500






      sql sql-server sql-server-2008






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 19:07









      Barbaros Özhan

      20.8k7 gold badges16 silver badges36 bronze badges




      20.8k7 gold badges16 silver badges36 bronze badges










      asked Mar 27 at 18:41









      shakeel shakeel

      457 bronze badges




      457 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          0















          You can use sum aggregation function recursively ( lag window analytic function cannot be used for sql server 2008 )



          with Expensis( ExpenseID, Date, Expense ) as
          (
          select 1, '2019-03-01', 2000 union all
          select 2, '2019-03-02', 1000 union all
          select 3, '2019-03-03', 500
          ), Income( IncomeID, Date, Income ) as
          (
          select 1, '2019-03-01', 10000 union all
          select 2, '2019-03-02', 13000 union all
          select 3, '2019-03-03', 10000
          ), t as
          (
          select i.date,
          i.income,
          e.expense,
          sum(i.income-e.expense) over (order by i.date) as closing_balance
          from income i
          join expensis e on e.date = i.date
          )
          select date,
          ( closing_balance - income + expense ) as opening_balance,
          income, expense, closing_balance
          from t;

          date opening balance income expense closing balance
          ---------- --------------- ------ ------- ---------------
          2019-03-01 0 10000 2000 8000
          2019-03-02 8000 13000 1000 20000
          2019-03-03 20000 10000 500 29500


          Demo






          share|improve this answer



























          • It works. Thanks a lot Barbaros Özhan

            – shakeel
            Mar 28 at 14:05











          • @shakeel you're welcome.

            – Barbaros Özhan
            Mar 28 at 15:01


















          0















          Here is one way you could do it
          You have to valuate income and expenses differently
          WITH INCOME AS
          (
          SELECT '2018-01-05' AS DT, 200 AS INC, 1 AS TP
          UNION ALL
          SELECT '2018-01-06' AS DT, 300 AS INC, 1 AS TP
          UNION ALL
          SELECT '2018-01-07' AS DT, 400 AS INC, 1 AS TP
          )



          , EXPENSES AS
          (
          SELECT '2018-01-05' AS DT, -100 AS EXPS, 2 AS TP
          UNION ALL
          SELECT '2018-01-06' AS DT, -500 AS EXPS, 2 AS TP
          UNION ALL
          SELECT '2018-01-07' AS DT, -30 AS EXPS, 2 AS TP
          )

          , UN AS
          (
          SELECT * FROM INCOME
          UNION ALL
          SELECT * FROM EXPENSES
          )

          SELECT *, [1]+[2] AS END_BALANCE FROM UN

          PIVOT
          (
          SUM(INC)
          FOR TP IN ([1],[2])
          ) AS P





          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%2f55384417%2fclosing-balance-of-the-previous-day-as-an-opening-balance-of-today%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0















            You can use sum aggregation function recursively ( lag window analytic function cannot be used for sql server 2008 )



            with Expensis( ExpenseID, Date, Expense ) as
            (
            select 1, '2019-03-01', 2000 union all
            select 2, '2019-03-02', 1000 union all
            select 3, '2019-03-03', 500
            ), Income( IncomeID, Date, Income ) as
            (
            select 1, '2019-03-01', 10000 union all
            select 2, '2019-03-02', 13000 union all
            select 3, '2019-03-03', 10000
            ), t as
            (
            select i.date,
            i.income,
            e.expense,
            sum(i.income-e.expense) over (order by i.date) as closing_balance
            from income i
            join expensis e on e.date = i.date
            )
            select date,
            ( closing_balance - income + expense ) as opening_balance,
            income, expense, closing_balance
            from t;

            date opening balance income expense closing balance
            ---------- --------------- ------ ------- ---------------
            2019-03-01 0 10000 2000 8000
            2019-03-02 8000 13000 1000 20000
            2019-03-03 20000 10000 500 29500


            Demo






            share|improve this answer



























            • It works. Thanks a lot Barbaros Özhan

              – shakeel
              Mar 28 at 14:05











            • @shakeel you're welcome.

              – Barbaros Özhan
              Mar 28 at 15:01















            0















            You can use sum aggregation function recursively ( lag window analytic function cannot be used for sql server 2008 )



            with Expensis( ExpenseID, Date, Expense ) as
            (
            select 1, '2019-03-01', 2000 union all
            select 2, '2019-03-02', 1000 union all
            select 3, '2019-03-03', 500
            ), Income( IncomeID, Date, Income ) as
            (
            select 1, '2019-03-01', 10000 union all
            select 2, '2019-03-02', 13000 union all
            select 3, '2019-03-03', 10000
            ), t as
            (
            select i.date,
            i.income,
            e.expense,
            sum(i.income-e.expense) over (order by i.date) as closing_balance
            from income i
            join expensis e on e.date = i.date
            )
            select date,
            ( closing_balance - income + expense ) as opening_balance,
            income, expense, closing_balance
            from t;

            date opening balance income expense closing balance
            ---------- --------------- ------ ------- ---------------
            2019-03-01 0 10000 2000 8000
            2019-03-02 8000 13000 1000 20000
            2019-03-03 20000 10000 500 29500


            Demo






            share|improve this answer



























            • It works. Thanks a lot Barbaros Özhan

              – shakeel
              Mar 28 at 14:05











            • @shakeel you're welcome.

              – Barbaros Özhan
              Mar 28 at 15:01













            0














            0










            0









            You can use sum aggregation function recursively ( lag window analytic function cannot be used for sql server 2008 )



            with Expensis( ExpenseID, Date, Expense ) as
            (
            select 1, '2019-03-01', 2000 union all
            select 2, '2019-03-02', 1000 union all
            select 3, '2019-03-03', 500
            ), Income( IncomeID, Date, Income ) as
            (
            select 1, '2019-03-01', 10000 union all
            select 2, '2019-03-02', 13000 union all
            select 3, '2019-03-03', 10000
            ), t as
            (
            select i.date,
            i.income,
            e.expense,
            sum(i.income-e.expense) over (order by i.date) as closing_balance
            from income i
            join expensis e on e.date = i.date
            )
            select date,
            ( closing_balance - income + expense ) as opening_balance,
            income, expense, closing_balance
            from t;

            date opening balance income expense closing balance
            ---------- --------------- ------ ------- ---------------
            2019-03-01 0 10000 2000 8000
            2019-03-02 8000 13000 1000 20000
            2019-03-03 20000 10000 500 29500


            Demo






            share|improve this answer















            You can use sum aggregation function recursively ( lag window analytic function cannot be used for sql server 2008 )



            with Expensis( ExpenseID, Date, Expense ) as
            (
            select 1, '2019-03-01', 2000 union all
            select 2, '2019-03-02', 1000 union all
            select 3, '2019-03-03', 500
            ), Income( IncomeID, Date, Income ) as
            (
            select 1, '2019-03-01', 10000 union all
            select 2, '2019-03-02', 13000 union all
            select 3, '2019-03-03', 10000
            ), t as
            (
            select i.date,
            i.income,
            e.expense,
            sum(i.income-e.expense) over (order by i.date) as closing_balance
            from income i
            join expensis e on e.date = i.date
            )
            select date,
            ( closing_balance - income + expense ) as opening_balance,
            income, expense, closing_balance
            from t;

            date opening balance income expense closing balance
            ---------- --------------- ------ ------- ---------------
            2019-03-01 0 10000 2000 8000
            2019-03-02 8000 13000 1000 20000
            2019-03-03 20000 10000 500 29500


            Demo







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 27 at 19:56

























            answered Mar 27 at 18:56









            Barbaros ÖzhanBarbaros Özhan

            20.8k7 gold badges16 silver badges36 bronze badges




            20.8k7 gold badges16 silver badges36 bronze badges















            • It works. Thanks a lot Barbaros Özhan

              – shakeel
              Mar 28 at 14:05











            • @shakeel you're welcome.

              – Barbaros Özhan
              Mar 28 at 15:01

















            • It works. Thanks a lot Barbaros Özhan

              – shakeel
              Mar 28 at 14:05











            • @shakeel you're welcome.

              – Barbaros Özhan
              Mar 28 at 15:01
















            It works. Thanks a lot Barbaros Özhan

            – shakeel
            Mar 28 at 14:05





            It works. Thanks a lot Barbaros Özhan

            – shakeel
            Mar 28 at 14:05













            @shakeel you're welcome.

            – Barbaros Özhan
            Mar 28 at 15:01





            @shakeel you're welcome.

            – Barbaros Özhan
            Mar 28 at 15:01













            0















            Here is one way you could do it
            You have to valuate income and expenses differently
            WITH INCOME AS
            (
            SELECT '2018-01-05' AS DT, 200 AS INC, 1 AS TP
            UNION ALL
            SELECT '2018-01-06' AS DT, 300 AS INC, 1 AS TP
            UNION ALL
            SELECT '2018-01-07' AS DT, 400 AS INC, 1 AS TP
            )



            , EXPENSES AS
            (
            SELECT '2018-01-05' AS DT, -100 AS EXPS, 2 AS TP
            UNION ALL
            SELECT '2018-01-06' AS DT, -500 AS EXPS, 2 AS TP
            UNION ALL
            SELECT '2018-01-07' AS DT, -30 AS EXPS, 2 AS TP
            )

            , UN AS
            (
            SELECT * FROM INCOME
            UNION ALL
            SELECT * FROM EXPENSES
            )

            SELECT *, [1]+[2] AS END_BALANCE FROM UN

            PIVOT
            (
            SUM(INC)
            FOR TP IN ([1],[2])
            ) AS P





            share|improve this answer





























              0















              Here is one way you could do it
              You have to valuate income and expenses differently
              WITH INCOME AS
              (
              SELECT '2018-01-05' AS DT, 200 AS INC, 1 AS TP
              UNION ALL
              SELECT '2018-01-06' AS DT, 300 AS INC, 1 AS TP
              UNION ALL
              SELECT '2018-01-07' AS DT, 400 AS INC, 1 AS TP
              )



              , EXPENSES AS
              (
              SELECT '2018-01-05' AS DT, -100 AS EXPS, 2 AS TP
              UNION ALL
              SELECT '2018-01-06' AS DT, -500 AS EXPS, 2 AS TP
              UNION ALL
              SELECT '2018-01-07' AS DT, -30 AS EXPS, 2 AS TP
              )

              , UN AS
              (
              SELECT * FROM INCOME
              UNION ALL
              SELECT * FROM EXPENSES
              )

              SELECT *, [1]+[2] AS END_BALANCE FROM UN

              PIVOT
              (
              SUM(INC)
              FOR TP IN ([1],[2])
              ) AS P





              share|improve this answer



























                0














                0










                0









                Here is one way you could do it
                You have to valuate income and expenses differently
                WITH INCOME AS
                (
                SELECT '2018-01-05' AS DT, 200 AS INC, 1 AS TP
                UNION ALL
                SELECT '2018-01-06' AS DT, 300 AS INC, 1 AS TP
                UNION ALL
                SELECT '2018-01-07' AS DT, 400 AS INC, 1 AS TP
                )



                , EXPENSES AS
                (
                SELECT '2018-01-05' AS DT, -100 AS EXPS, 2 AS TP
                UNION ALL
                SELECT '2018-01-06' AS DT, -500 AS EXPS, 2 AS TP
                UNION ALL
                SELECT '2018-01-07' AS DT, -30 AS EXPS, 2 AS TP
                )

                , UN AS
                (
                SELECT * FROM INCOME
                UNION ALL
                SELECT * FROM EXPENSES
                )

                SELECT *, [1]+[2] AS END_BALANCE FROM UN

                PIVOT
                (
                SUM(INC)
                FOR TP IN ([1],[2])
                ) AS P





                share|improve this answer













                Here is one way you could do it
                You have to valuate income and expenses differently
                WITH INCOME AS
                (
                SELECT '2018-01-05' AS DT, 200 AS INC, 1 AS TP
                UNION ALL
                SELECT '2018-01-06' AS DT, 300 AS INC, 1 AS TP
                UNION ALL
                SELECT '2018-01-07' AS DT, 400 AS INC, 1 AS TP
                )



                , EXPENSES AS
                (
                SELECT '2018-01-05' AS DT, -100 AS EXPS, 2 AS TP
                UNION ALL
                SELECT '2018-01-06' AS DT, -500 AS EXPS, 2 AS TP
                UNION ALL
                SELECT '2018-01-07' AS DT, -30 AS EXPS, 2 AS TP
                )

                , UN AS
                (
                SELECT * FROM INCOME
                UNION ALL
                SELECT * FROM EXPENSES
                )

                SELECT *, [1]+[2] AS END_BALANCE FROM UN

                PIVOT
                (
                SUM(INC)
                FOR TP IN ([1],[2])
                ) AS P






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 18:57









                Alex MAlex M

                1411 silver badge5 bronze badges




                1411 silver badge5 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%2f55384417%2fclosing-balance-of-the-previous-day-as-an-opening-balance-of-today%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

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현