How to calculate Day total as well as Monthly Total in the same Row










0















I need to get day total of Petty Cash (I'm already getting this) & need Monthly total relevant to Account Codes for today. Following I'm showing SQL for daily total. Someone Please help me to Calculate Monthly totals in the same row.



SELECT DPetAcNo as AcNo,
SUM(DPetAmount) as DayTotal
FROM PettyDetail
WHERE DPetComCode='15'
and DPetLocCode='01'
and DPetDate=CONVERT(date,'20181113',111)
GROUP BY DPetAcNo



Result for the above query show below



Result



According to the result 2018/11/13 AcNo 009111 Total = 22,995.00
and actual cumulative (2018/11/01 - 2018/11/13) total for 009111 = 136,265.42



Cumulative total










share|improve this question


























    0















    I need to get day total of Petty Cash (I'm already getting this) & need Monthly total relevant to Account Codes for today. Following I'm showing SQL for daily total. Someone Please help me to Calculate Monthly totals in the same row.



    SELECT DPetAcNo as AcNo,
    SUM(DPetAmount) as DayTotal
    FROM PettyDetail
    WHERE DPetComCode='15'
    and DPetLocCode='01'
    and DPetDate=CONVERT(date,'20181113',111)
    GROUP BY DPetAcNo



    Result for the above query show below



    Result



    According to the result 2018/11/13 AcNo 009111 Total = 22,995.00
    and actual cumulative (2018/11/01 - 2018/11/13) total for 009111 = 136,265.42



    Cumulative total










    share|improve this question
























      0












      0








      0








      I need to get day total of Petty Cash (I'm already getting this) & need Monthly total relevant to Account Codes for today. Following I'm showing SQL for daily total. Someone Please help me to Calculate Monthly totals in the same row.



      SELECT DPetAcNo as AcNo,
      SUM(DPetAmount) as DayTotal
      FROM PettyDetail
      WHERE DPetComCode='15'
      and DPetLocCode='01'
      and DPetDate=CONVERT(date,'20181113',111)
      GROUP BY DPetAcNo



      Result for the above query show below



      Result



      According to the result 2018/11/13 AcNo 009111 Total = 22,995.00
      and actual cumulative (2018/11/01 - 2018/11/13) total for 009111 = 136,265.42



      Cumulative total










      share|improve this question














      I need to get day total of Petty Cash (I'm already getting this) & need Monthly total relevant to Account Codes for today. Following I'm showing SQL for daily total. Someone Please help me to Calculate Monthly totals in the same row.



      SELECT DPetAcNo as AcNo,
      SUM(DPetAmount) as DayTotal
      FROM PettyDetail
      WHERE DPetComCode='15'
      and DPetLocCode='01'
      and DPetDate=CONVERT(date,'20181113',111)
      GROUP BY DPetAcNo



      Result for the above query show below



      Result



      According to the result 2018/11/13 AcNo 009111 Total = 22,995.00
      and actual cumulative (2018/11/01 - 2018/11/13) total for 009111 = 136,265.42



      Cumulative total







      sql sql-server tsql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 6:19









      MarkMark

      235




      235






















          4 Answers
          4






          active

          oldest

          votes


















          2














          Modified version of regbas’ answer that should perform better because it doesn’t consider so many rows:



          SELECT DPetAcNo as AcNo,
          SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
          SUM(DPetAmount) as MonthTotal
          FROM PettyDetail
          WHERE DPetComCode='15'
          and DPetLocCode='01'
          and DPetDate >= '2018-11-01'
          AND DPetDate < '2018-12-01'
          AND DPetAcNo IN (select dpetacno from pettydetail WHERE DPetComCode='15'
          and DPetLocCode='01'
          and DPetDate = '2018-11-13')
          GROUP BY DPetAcNo


          Sqlserver will implicitly convert a ‘yyyy-mm-dd’ string to a date if you want to skip the CONVERT code clutter. Having an index that covers date, comcode, loccode, account and includes amount will also help



          ——



          Another way to approach this is to use the queries you seem to have already written for daytotal and cumtotal, like this:



          Select * from
          (/*Insert your query that does daytotal into these brackets*/) d
          Inner join
          (/*Insert your query that does cumtotal into these brackets*/) c
          ON d.DPetAcNo = c.dpetacno





          share|improve this answer
































            1














            I am assuming your DPetDate is a Date or Datetime data type.



            You will need to filter the rows for the 1st of the month to the required date.
            for both day & month total on the same result, use CASE in the SUM ()



            SELECT DPetAcNo as AcNo,
            SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal,
            SUM(DPetAmount) as MonthTotal,
            FROM PettyDetail
            WHERE DPetComCode='15'
            and DPetLocCode='01'
            and DPetDate >= '20181101' -- from 1st of Nov
            and DPetDate <= '20181113' -- to 13 of Nov
            GROUP BY DPetAcNo


            EDIT 1:
            to only select AccNo based on the required date. Using subquery to get the Month Total. Or alternatively you can do it in a CROSS APPLY



            SELECT *,
            ( SELECT SUM(DPetAmount)
            FROM PettyDetail x
            WHERE x.DPetAcNo = p.AcNo
            AND x.DPetComCode = '15'
            AND x.DPetLocCode = '01'
            AND x.DPetDate >= '20180101'
            AND x.DPetDate <= '20181113') as MonthTotal
            FROM
            (
            SELECT DPetAcNo as AcNo,
            SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal
            FROM PettyDetail p
            WHERE DPetComCode='15'
            and DPetLocCode='01'
            and DPetDate = '20181113' -- for 13 of Nov only
            GROUP BY DPetAcNo
            ) p





            share|improve this answer




















            • 1





              “Great minds think alike.. ..and fools seldom differ!” :)

              – Caius Jard
              Nov 15 '18 at 6:38












            • Dear Squirrel, Thank you for your reply. Your result shows non today AcNo totals also. I need only today total as well as cumulative total for the relevant AcNo. Not for other AcNos

              – Mark
              Nov 15 '18 at 6:43











            • edited the answer. See the second query

              – Squirrel
              Nov 15 '18 at 6:51











            • Euww.. Putting a correlated query in a select block? Never would have expected you to do that!

              – Caius Jard
              Nov 15 '18 at 6:54











            • either that or a CROSS APPLY

              – Squirrel
              Nov 15 '18 at 6:55


















            0














            Try this



            SELECT DPetAcNo as AcNo,
            SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
            SUM(case when month(DPetDate)= 11 then DPetAmount else 0 end) as MonthTotal
            FROM PettyDetail
            WHERE DPetComCode='15'
            and DPetLocCode='01'
            GROUP BY DPetAcNo





            share|improve this answer























            • Dear RegBes, Yes I got the result. But I need only monthly total relevant to TODAY'S DPetAcNo only. Your solution calculate all table DPetAcNo relevant to the month of November.

              – Mark
              Nov 15 '18 at 6:31











            • @mark in fairness to regbas, 3 separate professionals all misinterpreted the question in the same way, so it possibly wasn’t a clear requirement.. check the updated answers (mine and squirrel) to address the requirements

              – Caius Jard
              Nov 15 '18 at 7:09



















            0














            How about a self join ?



            SELECT 
            DPetAcNo AcNo
            , DayTotal
            , SUM(DPetAmount) MonthlyTotal
            FROM PettyDetail pd
            JOIN (
            SELECT
            DPetAcNo AcNo
            , SUM(DPetAmount) DayTotal
            FROM
            PettyDetail
            WHERE
            DPetComCode='15'
            AND DPetLocCode='01'
            AND DPetDate = CONVERT(DATE,'20181113',111)
            GROUP BY DPetAcNo
            ) pd2 ON pd2.AcNo = dp.DPetAcNo
            WHERE
            MONTH(DPetDate) = MONTH(CONVERT(DATE,'20181113',111))
            AND YEAR(DPetDate) = YEAR(CONVERT(DATE,'20181113',111))
            GROUP BY DPetAcNo, DayTotal





            share|improve this answer

























            • Dear iSR5, your solution creates only AcNo and MonthlyTotal only. No DayTotal creates.

              – Mark
              Nov 15 '18 at 8:34











            • @Mark fixed, check now.

              – iSR5
              Nov 15 '18 at 8:40











            • Dear iSR5, yes now working

              – Mark
              Nov 15 '18 at 10:41










            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%2f53313520%2fhow-to-calculate-day-total-as-well-as-monthly-total-in-the-same-row%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Modified version of regbas’ answer that should perform better because it doesn’t consider so many rows:



            SELECT DPetAcNo as AcNo,
            SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
            SUM(DPetAmount) as MonthTotal
            FROM PettyDetail
            WHERE DPetComCode='15'
            and DPetLocCode='01'
            and DPetDate >= '2018-11-01'
            AND DPetDate < '2018-12-01'
            AND DPetAcNo IN (select dpetacno from pettydetail WHERE DPetComCode='15'
            and DPetLocCode='01'
            and DPetDate = '2018-11-13')
            GROUP BY DPetAcNo


            Sqlserver will implicitly convert a ‘yyyy-mm-dd’ string to a date if you want to skip the CONVERT code clutter. Having an index that covers date, comcode, loccode, account and includes amount will also help



            ——



            Another way to approach this is to use the queries you seem to have already written for daytotal and cumtotal, like this:



            Select * from
            (/*Insert your query that does daytotal into these brackets*/) d
            Inner join
            (/*Insert your query that does cumtotal into these brackets*/) c
            ON d.DPetAcNo = c.dpetacno





            share|improve this answer





























              2














              Modified version of regbas’ answer that should perform better because it doesn’t consider so many rows:



              SELECT DPetAcNo as AcNo,
              SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
              SUM(DPetAmount) as MonthTotal
              FROM PettyDetail
              WHERE DPetComCode='15'
              and DPetLocCode='01'
              and DPetDate >= '2018-11-01'
              AND DPetDate < '2018-12-01'
              AND DPetAcNo IN (select dpetacno from pettydetail WHERE DPetComCode='15'
              and DPetLocCode='01'
              and DPetDate = '2018-11-13')
              GROUP BY DPetAcNo


              Sqlserver will implicitly convert a ‘yyyy-mm-dd’ string to a date if you want to skip the CONVERT code clutter. Having an index that covers date, comcode, loccode, account and includes amount will also help



              ——



              Another way to approach this is to use the queries you seem to have already written for daytotal and cumtotal, like this:



              Select * from
              (/*Insert your query that does daytotal into these brackets*/) d
              Inner join
              (/*Insert your query that does cumtotal into these brackets*/) c
              ON d.DPetAcNo = c.dpetacno





              share|improve this answer



























                2












                2








                2







                Modified version of regbas’ answer that should perform better because it doesn’t consider so many rows:



                SELECT DPetAcNo as AcNo,
                SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
                SUM(DPetAmount) as MonthTotal
                FROM PettyDetail
                WHERE DPetComCode='15'
                and DPetLocCode='01'
                and DPetDate >= '2018-11-01'
                AND DPetDate < '2018-12-01'
                AND DPetAcNo IN (select dpetacno from pettydetail WHERE DPetComCode='15'
                and DPetLocCode='01'
                and DPetDate = '2018-11-13')
                GROUP BY DPetAcNo


                Sqlserver will implicitly convert a ‘yyyy-mm-dd’ string to a date if you want to skip the CONVERT code clutter. Having an index that covers date, comcode, loccode, account and includes amount will also help



                ——



                Another way to approach this is to use the queries you seem to have already written for daytotal and cumtotal, like this:



                Select * from
                (/*Insert your query that does daytotal into these brackets*/) d
                Inner join
                (/*Insert your query that does cumtotal into these brackets*/) c
                ON d.DPetAcNo = c.dpetacno





                share|improve this answer















                Modified version of regbas’ answer that should perform better because it doesn’t consider so many rows:



                SELECT DPetAcNo as AcNo,
                SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
                SUM(DPetAmount) as MonthTotal
                FROM PettyDetail
                WHERE DPetComCode='15'
                and DPetLocCode='01'
                and DPetDate >= '2018-11-01'
                AND DPetDate < '2018-12-01'
                AND DPetAcNo IN (select dpetacno from pettydetail WHERE DPetComCode='15'
                and DPetLocCode='01'
                and DPetDate = '2018-11-13')
                GROUP BY DPetAcNo


                Sqlserver will implicitly convert a ‘yyyy-mm-dd’ string to a date if you want to skip the CONVERT code clutter. Having an index that covers date, comcode, loccode, account and includes amount will also help



                ——



                Another way to approach this is to use the queries you seem to have already written for daytotal and cumtotal, like this:



                Select * from
                (/*Insert your query that does daytotal into these brackets*/) d
                Inner join
                (/*Insert your query that does cumtotal into these brackets*/) c
                ON d.DPetAcNo = c.dpetacno






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 6:51

























                answered Nov 15 '18 at 6:34









                Caius JardCaius Jard

                12.1k21240




                12.1k21240























                    1














                    I am assuming your DPetDate is a Date or Datetime data type.



                    You will need to filter the rows for the 1st of the month to the required date.
                    for both day & month total on the same result, use CASE in the SUM ()



                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal,
                    SUM(DPetAmount) as MonthTotal,
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate >= '20181101' -- from 1st of Nov
                    and DPetDate <= '20181113' -- to 13 of Nov
                    GROUP BY DPetAcNo


                    EDIT 1:
                    to only select AccNo based on the required date. Using subquery to get the Month Total. Or alternatively you can do it in a CROSS APPLY



                    SELECT *,
                    ( SELECT SUM(DPetAmount)
                    FROM PettyDetail x
                    WHERE x.DPetAcNo = p.AcNo
                    AND x.DPetComCode = '15'
                    AND x.DPetLocCode = '01'
                    AND x.DPetDate >= '20180101'
                    AND x.DPetDate <= '20181113') as MonthTotal
                    FROM
                    (
                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal
                    FROM PettyDetail p
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate = '20181113' -- for 13 of Nov only
                    GROUP BY DPetAcNo
                    ) p





                    share|improve this answer




















                    • 1





                      “Great minds think alike.. ..and fools seldom differ!” :)

                      – Caius Jard
                      Nov 15 '18 at 6:38












                    • Dear Squirrel, Thank you for your reply. Your result shows non today AcNo totals also. I need only today total as well as cumulative total for the relevant AcNo. Not for other AcNos

                      – Mark
                      Nov 15 '18 at 6:43











                    • edited the answer. See the second query

                      – Squirrel
                      Nov 15 '18 at 6:51











                    • Euww.. Putting a correlated query in a select block? Never would have expected you to do that!

                      – Caius Jard
                      Nov 15 '18 at 6:54











                    • either that or a CROSS APPLY

                      – Squirrel
                      Nov 15 '18 at 6:55















                    1














                    I am assuming your DPetDate is a Date or Datetime data type.



                    You will need to filter the rows for the 1st of the month to the required date.
                    for both day & month total on the same result, use CASE in the SUM ()



                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal,
                    SUM(DPetAmount) as MonthTotal,
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate >= '20181101' -- from 1st of Nov
                    and DPetDate <= '20181113' -- to 13 of Nov
                    GROUP BY DPetAcNo


                    EDIT 1:
                    to only select AccNo based on the required date. Using subquery to get the Month Total. Or alternatively you can do it in a CROSS APPLY



                    SELECT *,
                    ( SELECT SUM(DPetAmount)
                    FROM PettyDetail x
                    WHERE x.DPetAcNo = p.AcNo
                    AND x.DPetComCode = '15'
                    AND x.DPetLocCode = '01'
                    AND x.DPetDate >= '20180101'
                    AND x.DPetDate <= '20181113') as MonthTotal
                    FROM
                    (
                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal
                    FROM PettyDetail p
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate = '20181113' -- for 13 of Nov only
                    GROUP BY DPetAcNo
                    ) p





                    share|improve this answer




















                    • 1





                      “Great minds think alike.. ..and fools seldom differ!” :)

                      – Caius Jard
                      Nov 15 '18 at 6:38












                    • Dear Squirrel, Thank you for your reply. Your result shows non today AcNo totals also. I need only today total as well as cumulative total for the relevant AcNo. Not for other AcNos

                      – Mark
                      Nov 15 '18 at 6:43











                    • edited the answer. See the second query

                      – Squirrel
                      Nov 15 '18 at 6:51











                    • Euww.. Putting a correlated query in a select block? Never would have expected you to do that!

                      – Caius Jard
                      Nov 15 '18 at 6:54











                    • either that or a CROSS APPLY

                      – Squirrel
                      Nov 15 '18 at 6:55













                    1












                    1








                    1







                    I am assuming your DPetDate is a Date or Datetime data type.



                    You will need to filter the rows for the 1st of the month to the required date.
                    for both day & month total on the same result, use CASE in the SUM ()



                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal,
                    SUM(DPetAmount) as MonthTotal,
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate >= '20181101' -- from 1st of Nov
                    and DPetDate <= '20181113' -- to 13 of Nov
                    GROUP BY DPetAcNo


                    EDIT 1:
                    to only select AccNo based on the required date. Using subquery to get the Month Total. Or alternatively you can do it in a CROSS APPLY



                    SELECT *,
                    ( SELECT SUM(DPetAmount)
                    FROM PettyDetail x
                    WHERE x.DPetAcNo = p.AcNo
                    AND x.DPetComCode = '15'
                    AND x.DPetLocCode = '01'
                    AND x.DPetDate >= '20180101'
                    AND x.DPetDate <= '20181113') as MonthTotal
                    FROM
                    (
                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal
                    FROM PettyDetail p
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate = '20181113' -- for 13 of Nov only
                    GROUP BY DPetAcNo
                    ) p





                    share|improve this answer















                    I am assuming your DPetDate is a Date or Datetime data type.



                    You will need to filter the rows for the 1st of the month to the required date.
                    for both day & month total on the same result, use CASE in the SUM ()



                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal,
                    SUM(DPetAmount) as MonthTotal,
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate >= '20181101' -- from 1st of Nov
                    and DPetDate <= '20181113' -- to 13 of Nov
                    GROUP BY DPetAcNo


                    EDIT 1:
                    to only select AccNo based on the required date. Using subquery to get the Month Total. Or alternatively you can do it in a CROSS APPLY



                    SELECT *,
                    ( SELECT SUM(DPetAmount)
                    FROM PettyDetail x
                    WHERE x.DPetAcNo = p.AcNo
                    AND x.DPetComCode = '15'
                    AND x.DPetLocCode = '01'
                    AND x.DPetDate >= '20180101'
                    AND x.DPetDate <= '20181113') as MonthTotal
                    FROM
                    (
                    SELECT DPetAcNo as AcNo,
                    SUM(CASE WHEN DPetDate = '20181113' THEN DPetAmount ELSE 0 END) as DayTotal
                    FROM PettyDetail p
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    and DPetDate = '20181113' -- for 13 of Nov only
                    GROUP BY DPetAcNo
                    ) p






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 '18 at 6:55

























                    answered Nov 15 '18 at 6:28









                    SquirrelSquirrel

                    11.9k22128




                    11.9k22128







                    • 1





                      “Great minds think alike.. ..and fools seldom differ!” :)

                      – Caius Jard
                      Nov 15 '18 at 6:38












                    • Dear Squirrel, Thank you for your reply. Your result shows non today AcNo totals also. I need only today total as well as cumulative total for the relevant AcNo. Not for other AcNos

                      – Mark
                      Nov 15 '18 at 6:43











                    • edited the answer. See the second query

                      – Squirrel
                      Nov 15 '18 at 6:51











                    • Euww.. Putting a correlated query in a select block? Never would have expected you to do that!

                      – Caius Jard
                      Nov 15 '18 at 6:54











                    • either that or a CROSS APPLY

                      – Squirrel
                      Nov 15 '18 at 6:55












                    • 1





                      “Great minds think alike.. ..and fools seldom differ!” :)

                      – Caius Jard
                      Nov 15 '18 at 6:38












                    • Dear Squirrel, Thank you for your reply. Your result shows non today AcNo totals also. I need only today total as well as cumulative total for the relevant AcNo. Not for other AcNos

                      – Mark
                      Nov 15 '18 at 6:43











                    • edited the answer. See the second query

                      – Squirrel
                      Nov 15 '18 at 6:51











                    • Euww.. Putting a correlated query in a select block? Never would have expected you to do that!

                      – Caius Jard
                      Nov 15 '18 at 6:54











                    • either that or a CROSS APPLY

                      – Squirrel
                      Nov 15 '18 at 6:55







                    1




                    1





                    “Great minds think alike.. ..and fools seldom differ!” :)

                    – Caius Jard
                    Nov 15 '18 at 6:38






                    “Great minds think alike.. ..and fools seldom differ!” :)

                    – Caius Jard
                    Nov 15 '18 at 6:38














                    Dear Squirrel, Thank you for your reply. Your result shows non today AcNo totals also. I need only today total as well as cumulative total for the relevant AcNo. Not for other AcNos

                    – Mark
                    Nov 15 '18 at 6:43





                    Dear Squirrel, Thank you for your reply. Your result shows non today AcNo totals also. I need only today total as well as cumulative total for the relevant AcNo. Not for other AcNos

                    – Mark
                    Nov 15 '18 at 6:43













                    edited the answer. See the second query

                    – Squirrel
                    Nov 15 '18 at 6:51





                    edited the answer. See the second query

                    – Squirrel
                    Nov 15 '18 at 6:51













                    Euww.. Putting a correlated query in a select block? Never would have expected you to do that!

                    – Caius Jard
                    Nov 15 '18 at 6:54





                    Euww.. Putting a correlated query in a select block? Never would have expected you to do that!

                    – Caius Jard
                    Nov 15 '18 at 6:54













                    either that or a CROSS APPLY

                    – Squirrel
                    Nov 15 '18 at 6:55





                    either that or a CROSS APPLY

                    – Squirrel
                    Nov 15 '18 at 6:55











                    0














                    Try this



                    SELECT DPetAcNo as AcNo,
                    SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
                    SUM(case when month(DPetDate)= 11 then DPetAmount else 0 end) as MonthTotal
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    GROUP BY DPetAcNo





                    share|improve this answer























                    • Dear RegBes, Yes I got the result. But I need only monthly total relevant to TODAY'S DPetAcNo only. Your solution calculate all table DPetAcNo relevant to the month of November.

                      – Mark
                      Nov 15 '18 at 6:31











                    • @mark in fairness to regbas, 3 separate professionals all misinterpreted the question in the same way, so it possibly wasn’t a clear requirement.. check the updated answers (mine and squirrel) to address the requirements

                      – Caius Jard
                      Nov 15 '18 at 7:09
















                    0














                    Try this



                    SELECT DPetAcNo as AcNo,
                    SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
                    SUM(case when month(DPetDate)= 11 then DPetAmount else 0 end) as MonthTotal
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    GROUP BY DPetAcNo





                    share|improve this answer























                    • Dear RegBes, Yes I got the result. But I need only monthly total relevant to TODAY'S DPetAcNo only. Your solution calculate all table DPetAcNo relevant to the month of November.

                      – Mark
                      Nov 15 '18 at 6:31











                    • @mark in fairness to regbas, 3 separate professionals all misinterpreted the question in the same way, so it possibly wasn’t a clear requirement.. check the updated answers (mine and squirrel) to address the requirements

                      – Caius Jard
                      Nov 15 '18 at 7:09














                    0












                    0








                    0







                    Try this



                    SELECT DPetAcNo as AcNo,
                    SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
                    SUM(case when month(DPetDate)= 11 then DPetAmount else 0 end) as MonthTotal
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    GROUP BY DPetAcNo





                    share|improve this answer













                    Try this



                    SELECT DPetAcNo as AcNo,
                    SUM(case when DPetDate=CONVERT(date,'20181113',111) then DPetAmount else 0 end) as DayTotal,
                    SUM(case when month(DPetDate)= 11 then DPetAmount else 0 end) as MonthTotal
                    FROM PettyDetail
                    WHERE DPetComCode='15'
                    and DPetLocCode='01'
                    GROUP BY DPetAcNo






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 6:23









                    RegBesRegBes

                    47129




                    47129












                    • Dear RegBes, Yes I got the result. But I need only monthly total relevant to TODAY'S DPetAcNo only. Your solution calculate all table DPetAcNo relevant to the month of November.

                      – Mark
                      Nov 15 '18 at 6:31











                    • @mark in fairness to regbas, 3 separate professionals all misinterpreted the question in the same way, so it possibly wasn’t a clear requirement.. check the updated answers (mine and squirrel) to address the requirements

                      – Caius Jard
                      Nov 15 '18 at 7:09


















                    • Dear RegBes, Yes I got the result. But I need only monthly total relevant to TODAY'S DPetAcNo only. Your solution calculate all table DPetAcNo relevant to the month of November.

                      – Mark
                      Nov 15 '18 at 6:31











                    • @mark in fairness to regbas, 3 separate professionals all misinterpreted the question in the same way, so it possibly wasn’t a clear requirement.. check the updated answers (mine and squirrel) to address the requirements

                      – Caius Jard
                      Nov 15 '18 at 7:09

















                    Dear RegBes, Yes I got the result. But I need only monthly total relevant to TODAY'S DPetAcNo only. Your solution calculate all table DPetAcNo relevant to the month of November.

                    – Mark
                    Nov 15 '18 at 6:31





                    Dear RegBes, Yes I got the result. But I need only monthly total relevant to TODAY'S DPetAcNo only. Your solution calculate all table DPetAcNo relevant to the month of November.

                    – Mark
                    Nov 15 '18 at 6:31













                    @mark in fairness to regbas, 3 separate professionals all misinterpreted the question in the same way, so it possibly wasn’t a clear requirement.. check the updated answers (mine and squirrel) to address the requirements

                    – Caius Jard
                    Nov 15 '18 at 7:09






                    @mark in fairness to regbas, 3 separate professionals all misinterpreted the question in the same way, so it possibly wasn’t a clear requirement.. check the updated answers (mine and squirrel) to address the requirements

                    – Caius Jard
                    Nov 15 '18 at 7:09












                    0














                    How about a self join ?



                    SELECT 
                    DPetAcNo AcNo
                    , DayTotal
                    , SUM(DPetAmount) MonthlyTotal
                    FROM PettyDetail pd
                    JOIN (
                    SELECT
                    DPetAcNo AcNo
                    , SUM(DPetAmount) DayTotal
                    FROM
                    PettyDetail
                    WHERE
                    DPetComCode='15'
                    AND DPetLocCode='01'
                    AND DPetDate = CONVERT(DATE,'20181113',111)
                    GROUP BY DPetAcNo
                    ) pd2 ON pd2.AcNo = dp.DPetAcNo
                    WHERE
                    MONTH(DPetDate) = MONTH(CONVERT(DATE,'20181113',111))
                    AND YEAR(DPetDate) = YEAR(CONVERT(DATE,'20181113',111))
                    GROUP BY DPetAcNo, DayTotal





                    share|improve this answer

























                    • Dear iSR5, your solution creates only AcNo and MonthlyTotal only. No DayTotal creates.

                      – Mark
                      Nov 15 '18 at 8:34











                    • @Mark fixed, check now.

                      – iSR5
                      Nov 15 '18 at 8:40











                    • Dear iSR5, yes now working

                      – Mark
                      Nov 15 '18 at 10:41















                    0














                    How about a self join ?



                    SELECT 
                    DPetAcNo AcNo
                    , DayTotal
                    , SUM(DPetAmount) MonthlyTotal
                    FROM PettyDetail pd
                    JOIN (
                    SELECT
                    DPetAcNo AcNo
                    , SUM(DPetAmount) DayTotal
                    FROM
                    PettyDetail
                    WHERE
                    DPetComCode='15'
                    AND DPetLocCode='01'
                    AND DPetDate = CONVERT(DATE,'20181113',111)
                    GROUP BY DPetAcNo
                    ) pd2 ON pd2.AcNo = dp.DPetAcNo
                    WHERE
                    MONTH(DPetDate) = MONTH(CONVERT(DATE,'20181113',111))
                    AND YEAR(DPetDate) = YEAR(CONVERT(DATE,'20181113',111))
                    GROUP BY DPetAcNo, DayTotal





                    share|improve this answer

























                    • Dear iSR5, your solution creates only AcNo and MonthlyTotal only. No DayTotal creates.

                      – Mark
                      Nov 15 '18 at 8:34











                    • @Mark fixed, check now.

                      – iSR5
                      Nov 15 '18 at 8:40











                    • Dear iSR5, yes now working

                      – Mark
                      Nov 15 '18 at 10:41













                    0












                    0








                    0







                    How about a self join ?



                    SELECT 
                    DPetAcNo AcNo
                    , DayTotal
                    , SUM(DPetAmount) MonthlyTotal
                    FROM PettyDetail pd
                    JOIN (
                    SELECT
                    DPetAcNo AcNo
                    , SUM(DPetAmount) DayTotal
                    FROM
                    PettyDetail
                    WHERE
                    DPetComCode='15'
                    AND DPetLocCode='01'
                    AND DPetDate = CONVERT(DATE,'20181113',111)
                    GROUP BY DPetAcNo
                    ) pd2 ON pd2.AcNo = dp.DPetAcNo
                    WHERE
                    MONTH(DPetDate) = MONTH(CONVERT(DATE,'20181113',111))
                    AND YEAR(DPetDate) = YEAR(CONVERT(DATE,'20181113',111))
                    GROUP BY DPetAcNo, DayTotal





                    share|improve this answer















                    How about a self join ?



                    SELECT 
                    DPetAcNo AcNo
                    , DayTotal
                    , SUM(DPetAmount) MonthlyTotal
                    FROM PettyDetail pd
                    JOIN (
                    SELECT
                    DPetAcNo AcNo
                    , SUM(DPetAmount) DayTotal
                    FROM
                    PettyDetail
                    WHERE
                    DPetComCode='15'
                    AND DPetLocCode='01'
                    AND DPetDate = CONVERT(DATE,'20181113',111)
                    GROUP BY DPetAcNo
                    ) pd2 ON pd2.AcNo = dp.DPetAcNo
                    WHERE
                    MONTH(DPetDate) = MONTH(CONVERT(DATE,'20181113',111))
                    AND YEAR(DPetDate) = YEAR(CONVERT(DATE,'20181113',111))
                    GROUP BY DPetAcNo, DayTotal






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 '18 at 8:39

























                    answered Nov 15 '18 at 7:58









                    iSR5iSR5

                    1,563278




                    1,563278












                    • Dear iSR5, your solution creates only AcNo and MonthlyTotal only. No DayTotal creates.

                      – Mark
                      Nov 15 '18 at 8:34











                    • @Mark fixed, check now.

                      – iSR5
                      Nov 15 '18 at 8:40











                    • Dear iSR5, yes now working

                      – Mark
                      Nov 15 '18 at 10:41

















                    • Dear iSR5, your solution creates only AcNo and MonthlyTotal only. No DayTotal creates.

                      – Mark
                      Nov 15 '18 at 8:34











                    • @Mark fixed, check now.

                      – iSR5
                      Nov 15 '18 at 8:40











                    • Dear iSR5, yes now working

                      – Mark
                      Nov 15 '18 at 10:41
















                    Dear iSR5, your solution creates only AcNo and MonthlyTotal only. No DayTotal creates.

                    – Mark
                    Nov 15 '18 at 8:34





                    Dear iSR5, your solution creates only AcNo and MonthlyTotal only. No DayTotal creates.

                    – Mark
                    Nov 15 '18 at 8:34













                    @Mark fixed, check now.

                    – iSR5
                    Nov 15 '18 at 8:40





                    @Mark fixed, check now.

                    – iSR5
                    Nov 15 '18 at 8:40













                    Dear iSR5, yes now working

                    – Mark
                    Nov 15 '18 at 10:41





                    Dear iSR5, yes now working

                    – Mark
                    Nov 15 '18 at 10:41

















                    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%2f53313520%2fhow-to-calculate-day-total-as-well-as-monthly-total-in-the-same-row%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







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto