How to get sum & latest activity in same result?











up vote
2
down vote

favorite












Table_1
Name | Activity | LogTime
A | 0 | 2018-12-17 10:16:04.877
A | 1 | 2018-12-15 10:16:04.877
A | 0 | 2018-12-16 10:16:04.877
A | 0 | 2018-12-10 10:16:04.877
A | 0 | 2018-12-10 10:10:04.877
B | 1 | 2018-12-16 10:16:04.877
B | 0 | 2018-12-17 10:16:04.877
C | 1 | 2018-12-14 10:16:04.877
C | 1 | 2018-12-12 10:16:04.877
C | 1 | 2018-12-18 10:16:04.877


Desired Result



Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
C | 0 | 3 | NULL | 2018-12-18 10:16:04.877


I am able to get result as below



Name |   TOTALActivity_0  | TOTALActivity_1
A | 4 | 1
B | 1 | 1
C | 0 | 3


Using below query



SELECT 
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
FROM Table_1
GROUP BY NAME


If I tried as below



 SELECT 
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME,Activity,LogTime


Then it is giving Single-single & redundant records.
Then I tried with again with below query



 SELECT 
NAME,
SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
NULL AS LatestActivity_0_Logtime,
NULL AS LatestActivity_1_Logtime
FROM Table_1
GROUP BY NAME
UNION
SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
FROM Table_1


Then result is again undesirable as below



Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
A | 4 | 1 | NULL | NULL
B | 1 | 1 | NULL | NULL
B | 1 | 3 | NULL | NULL
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
.
.
.
.
.
.


Please Help to get distinct & merged result as desired










share|improve this question




























    up vote
    2
    down vote

    favorite












    Table_1
    Name | Activity | LogTime
    A | 0 | 2018-12-17 10:16:04.877
    A | 1 | 2018-12-15 10:16:04.877
    A | 0 | 2018-12-16 10:16:04.877
    A | 0 | 2018-12-10 10:16:04.877
    A | 0 | 2018-12-10 10:10:04.877
    B | 1 | 2018-12-16 10:16:04.877
    B | 0 | 2018-12-17 10:16:04.877
    C | 1 | 2018-12-14 10:16:04.877
    C | 1 | 2018-12-12 10:16:04.877
    C | 1 | 2018-12-18 10:16:04.877


    Desired Result



    Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
    A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
    B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
    C | 0 | 3 | NULL | 2018-12-18 10:16:04.877


    I am able to get result as below



    Name |   TOTALActivity_0  | TOTALActivity_1
    A | 4 | 1
    B | 1 | 1
    C | 0 | 3


    Using below query



    SELECT 
    NAME,
    SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
    SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
    FROM Table_1
    GROUP BY NAME


    If I tried as below



     SELECT 
    NAME,
    SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
    SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
    CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
    CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
    FROM Table_1
    GROUP BY NAME,Activity,LogTime


    Then it is giving Single-single & redundant records.
    Then I tried with again with below query



     SELECT 
    NAME,
    SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
    SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
    NULL AS LatestActivity_0_Logtime,
    NULL AS LatestActivity_1_Logtime
    FROM Table_1
    GROUP BY NAME
    UNION
    SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
    CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
    FROM Table_1


    Then result is again undesirable as below



    Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
    A | 4 | 1 | NULL | NULL
    B | 1 | 1 | NULL | NULL
    B | 1 | 3 | NULL | NULL
    NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
    NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
    NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
    NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
    NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
    .
    .
    .
    .
    .
    .


    Please Help to get distinct & merged result as desired










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Table_1
      Name | Activity | LogTime
      A | 0 | 2018-12-17 10:16:04.877
      A | 1 | 2018-12-15 10:16:04.877
      A | 0 | 2018-12-16 10:16:04.877
      A | 0 | 2018-12-10 10:16:04.877
      A | 0 | 2018-12-10 10:10:04.877
      B | 1 | 2018-12-16 10:16:04.877
      B | 0 | 2018-12-17 10:16:04.877
      C | 1 | 2018-12-14 10:16:04.877
      C | 1 | 2018-12-12 10:16:04.877
      C | 1 | 2018-12-18 10:16:04.877


      Desired Result



      Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
      A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
      B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
      C | 0 | 3 | NULL | 2018-12-18 10:16:04.877


      I am able to get result as below



      Name |   TOTALActivity_0  | TOTALActivity_1
      A | 4 | 1
      B | 1 | 1
      C | 0 | 3


      Using below query



      SELECT 
      NAME,
      SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
      SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
      FROM Table_1
      GROUP BY NAME


      If I tried as below



       SELECT 
      NAME,
      SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
      SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
      CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
      CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
      FROM Table_1
      GROUP BY NAME,Activity,LogTime


      Then it is giving Single-single & redundant records.
      Then I tried with again with below query



       SELECT 
      NAME,
      SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
      SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
      NULL AS LatestActivity_0_Logtime,
      NULL AS LatestActivity_1_Logtime
      FROM Table_1
      GROUP BY NAME
      UNION
      SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
      CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
      FROM Table_1


      Then result is again undesirable as below



      Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
      A | 4 | 1 | NULL | NULL
      B | 1 | 1 | NULL | NULL
      B | 1 | 3 | NULL | NULL
      NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
      NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
      NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
      NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
      NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
      .
      .
      .
      .
      .
      .


      Please Help to get distinct & merged result as desired










      share|improve this question















      Table_1
      Name | Activity | LogTime
      A | 0 | 2018-12-17 10:16:04.877
      A | 1 | 2018-12-15 10:16:04.877
      A | 0 | 2018-12-16 10:16:04.877
      A | 0 | 2018-12-10 10:16:04.877
      A | 0 | 2018-12-10 10:10:04.877
      B | 1 | 2018-12-16 10:16:04.877
      B | 0 | 2018-12-17 10:16:04.877
      C | 1 | 2018-12-14 10:16:04.877
      C | 1 | 2018-12-12 10:16:04.877
      C | 1 | 2018-12-18 10:16:04.877


      Desired Result



      Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
      A | 4 | 1 | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
      B | 1 | 1 | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
      C | 0 | 3 | NULL | 2018-12-18 10:16:04.877


      I am able to get result as below



      Name |   TOTALActivity_0  | TOTALActivity_1
      A | 4 | 1
      B | 1 | 1
      C | 0 | 3


      Using below query



      SELECT 
      NAME,
      SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
      SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1
      FROM Table_1
      GROUP BY NAME


      If I tried as below



       SELECT 
      NAME,
      SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
      SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
      CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
      CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
      FROM Table_1
      GROUP BY NAME,Activity,LogTime


      Then it is giving Single-single & redundant records.
      Then I tried with again with below query



       SELECT 
      NAME,
      SUM(CASE WHEN Activity = 0 THEN 1 ELSE 0 END) TOTALActivity_0,
      SUM(CASE WHEN Activity = 1 THEN 1 ELSE 0 END) TOTALActivity_1,
      NULL AS LatestActivity_0_Logtime,
      NULL AS LatestActivity_1_Logtime
      FROM Table_1
      GROUP BY NAME
      UNION
      SELECT NULL,NULL,NULL,CASE WHEN Activity = 0 THEN LogTime ELSE NULL END AS LatestActivity_0_Logtime,
      CASE WHEN Activity = 1 THEN LogTime ELSE NULL END AS LatestActivity_1_Logtime
      FROM Table_1


      Then result is again undesirable as below



      Name |   TOTALActivity_0  | TOTALActivity_1  |  LatestActivity_0_Logtime  | LatestActivity_1_Logtime
      A | 4 | 1 | NULL | NULL
      B | 1 | 1 | NULL | NULL
      B | 1 | 3 | NULL | NULL
      NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-15 10:16:04.877
      NULL | NULL | NULL | 2018-12-16 10:16:04.877 | 2018-12-16 10:16:04.877
      NULL | NULL | NULL | 2018-12-10 10:16:04.877 | 2018-12-18 10:16:04.877
      NULL | NULL | NULL | 2018-12-10 10:10:04.877 | 2018-12-15 10:16:04.877
      NULL | NULL | NULL | 2018-12-17 10:16:04.877 | 2018-12-16 10:16:04.877
      .
      .
      .
      .
      .
      .


      Please Help to get distinct & merged result as desired







      sql-server sql-server-2017






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago

























      asked 1 hour ago









      Hina Khuman

      596




      596






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          This should work:



          SELECT 
          Name,
          COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
          COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
          MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
          MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
          FROM
          Table_1
          GROUP BY
          Name ;


          Test in: dbfiddle.uk






          share|improve this answer























          • Wow, I tried using function which was obviously not optimal. Great,thanks
            – Hina Khuman
            34 mins ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fdba.stackexchange.com%2fquestions%2f225145%2fhow-to-get-sum-latest-activity-in-same-result%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








          up vote
          3
          down vote



          accepted










          This should work:



          SELECT 
          Name,
          COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
          COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
          MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
          MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
          FROM
          Table_1
          GROUP BY
          Name ;


          Test in: dbfiddle.uk






          share|improve this answer























          • Wow, I tried using function which was obviously not optimal. Great,thanks
            – Hina Khuman
            34 mins ago















          up vote
          3
          down vote



          accepted










          This should work:



          SELECT 
          Name,
          COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
          COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
          MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
          MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
          FROM
          Table_1
          GROUP BY
          Name ;


          Test in: dbfiddle.uk






          share|improve this answer























          • Wow, I tried using function which was obviously not optimal. Great,thanks
            – Hina Khuman
            34 mins ago













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          This should work:



          SELECT 
          Name,
          COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
          COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
          MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
          MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
          FROM
          Table_1
          GROUP BY
          Name ;


          Test in: dbfiddle.uk






          share|improve this answer














          This should work:



          SELECT 
          Name,
          COUNT(CASE WHEN Activity = 0 THEN 1 END) AS TOTALActivity_0,
          COUNT(CASE WHEN Activity = 1 THEN 1 END) AS TOTALActivity_1,
          MAX(CASE WHEN Activity = 0 THEN LogTime END) AS LatestActivity_0_Logtime,
          MAX(CASE WHEN Activity = 1 THEN LogTime END) AS LatestActivity_1_Logtime
          FROM
          Table_1
          GROUP BY
          Name ;


          Test in: dbfiddle.uk







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          ypercubeᵀᴹ

          73.9k11124204




          73.9k11124204












          • Wow, I tried using function which was obviously not optimal. Great,thanks
            – Hina Khuman
            34 mins ago


















          • Wow, I tried using function which was obviously not optimal. Great,thanks
            – Hina Khuman
            34 mins ago
















          Wow, I tried using function which was obviously not optimal. Great,thanks
          – Hina Khuman
          34 mins ago




          Wow, I tried using function which was obviously not optimal. Great,thanks
          – Hina Khuman
          34 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fdba.stackexchange.com%2fquestions%2f225145%2fhow-to-get-sum-latest-activity-in-same-result%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

          Ellipse (mathématiques)

          Quarter-circle Tiles

          Mont Emei