Select Datetime and group by Datetime Query












0















I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.



SELECT convert(date,a.time)  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)


Result I get:



Result



The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.



SELECT a.time  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time


datetime










share|improve this question




















  • 2





    provider your sample data and expected output in table format

    – fa06
    Nov 20 '18 at 5:29











  • already edited and added some pics

    – Miku Hatsune
    Nov 20 '18 at 6:11











  • I want to get is only the maximum data per the day based on what column ? va or vb ?

    – Squirrel
    Nov 20 '18 at 6:26













  • both columns. @Squirrel

    – Miku Hatsune
    Nov 20 '18 at 6:29
















0















I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.



SELECT convert(date,a.time)  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)


Result I get:



Result



The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.



SELECT a.time  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time


datetime










share|improve this question




















  • 2





    provider your sample data and expected output in table format

    – fa06
    Nov 20 '18 at 5:29











  • already edited and added some pics

    – Miku Hatsune
    Nov 20 '18 at 6:11











  • I want to get is only the maximum data per the day based on what column ? va or vb ?

    – Squirrel
    Nov 20 '18 at 6:26













  • both columns. @Squirrel

    – Miku Hatsune
    Nov 20 '18 at 6:29














0












0








0








I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.



SELECT convert(date,a.time)  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)


Result I get:



Result



The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.



SELECT a.time  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time


datetime










share|improve this question
















I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.



SELECT convert(date,a.time)  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)


Result I get:



Result



The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.



SELECT a.time  AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time


datetime







sql sql-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 6:10







Miku Hatsune

















asked Nov 20 '18 at 5:26









Miku HatsuneMiku Hatsune

164




164








  • 2





    provider your sample data and expected output in table format

    – fa06
    Nov 20 '18 at 5:29











  • already edited and added some pics

    – Miku Hatsune
    Nov 20 '18 at 6:11











  • I want to get is only the maximum data per the day based on what column ? va or vb ?

    – Squirrel
    Nov 20 '18 at 6:26













  • both columns. @Squirrel

    – Miku Hatsune
    Nov 20 '18 at 6:29














  • 2





    provider your sample data and expected output in table format

    – fa06
    Nov 20 '18 at 5:29











  • already edited and added some pics

    – Miku Hatsune
    Nov 20 '18 at 6:11











  • I want to get is only the maximum data per the day based on what column ? va or vb ?

    – Squirrel
    Nov 20 '18 at 6:26













  • both columns. @Squirrel

    – Miku Hatsune
    Nov 20 '18 at 6:29








2




2





provider your sample data and expected output in table format

– fa06
Nov 20 '18 at 5:29





provider your sample data and expected output in table format

– fa06
Nov 20 '18 at 5:29













already edited and added some pics

– Miku Hatsune
Nov 20 '18 at 6:11





already edited and added some pics

– Miku Hatsune
Nov 20 '18 at 6:11













I want to get is only the maximum data per the day based on what column ? va or vb ?

– Squirrel
Nov 20 '18 at 6:26







I want to get is only the maximum data per the day based on what column ? va or vb ?

– Squirrel
Nov 20 '18 at 6:26















both columns. @Squirrel

– Miku Hatsune
Nov 20 '18 at 6:29





both columns. @Squirrel

– Miku Hatsune
Nov 20 '18 at 6:29












3 Answers
3






active

oldest

votes


















0














use row_number() window function. partition by dateadd(day, datediff(day, 0, a.time), 0) will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va follow by Vb



SELECT *
FROM
(
SELECT *,
rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
order by a.Va desc, a.Vb desc)
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
) d
WHERE d.rn = 1





share|improve this answer































    0














    You can try using correlated subquery



    SELECT a.time  AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
    FROM tbl_sample a
    WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
    and exists
    (select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
    group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
    )





    share|improve this answer































      -1














      you can simply use CONVERT(datetime,a.time) in place of CONVERT(date,a.time) in your query.



      if your requirement something different then could you please send some example data, right now i am not much clear.






      share|improve this answer
























      • the column time is already in datetime format but when the result I get is not correct.

        – Miku Hatsune
        Nov 20 '18 at 6:11











      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%2f53386732%2fselect-datetime-and-group-by-datetime-query%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      use row_number() window function. partition by dateadd(day, datediff(day, 0, a.time), 0) will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va follow by Vb



      SELECT *
      FROM
      (
      SELECT *,
      rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
      order by a.Va desc, a.Vb desc)
      FROM tbl_sample a
      WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
      ) d
      WHERE d.rn = 1





      share|improve this answer




























        0














        use row_number() window function. partition by dateadd(day, datediff(day, 0, a.time), 0) will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va follow by Vb



        SELECT *
        FROM
        (
        SELECT *,
        rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
        order by a.Va desc, a.Vb desc)
        FROM tbl_sample a
        WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
        ) d
        WHERE d.rn = 1





        share|improve this answer


























          0












          0








          0







          use row_number() window function. partition by dateadd(day, datediff(day, 0, a.time), 0) will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va follow by Vb



          SELECT *
          FROM
          (
          SELECT *,
          rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
          order by a.Va desc, a.Vb desc)
          FROM tbl_sample a
          WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
          ) d
          WHERE d.rn = 1





          share|improve this answer













          use row_number() window function. partition by dateadd(day, datediff(day, 0, a.time), 0) will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va follow by Vb



          SELECT *
          FROM
          (
          SELECT *,
          rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
          order by a.Va desc, a.Vb desc)
          FROM tbl_sample a
          WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
          ) d
          WHERE d.rn = 1






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 6:28









          SquirrelSquirrel

          11.8k22027




          11.8k22027

























              0














              You can try using correlated subquery



              SELECT a.time  AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
              FROM tbl_sample a
              WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
              and exists
              (select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
              group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
              )





              share|improve this answer




























                0














                You can try using correlated subquery



                SELECT a.time  AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
                FROM tbl_sample a
                WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
                and exists
                (select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
                group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
                )





                share|improve this answer


























                  0












                  0








                  0







                  You can try using correlated subquery



                  SELECT a.time  AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
                  FROM tbl_sample a
                  WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
                  and exists
                  (select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
                  group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
                  )





                  share|improve this answer













                  You can try using correlated subquery



                  SELECT a.time  AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
                  FROM tbl_sample a
                  WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
                  and exists
                  (select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
                  group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
                  )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 6:28









                  fa06fa06

                  11.8k2917




                  11.8k2917























                      -1














                      you can simply use CONVERT(datetime,a.time) in place of CONVERT(date,a.time) in your query.



                      if your requirement something different then could you please send some example data, right now i am not much clear.






                      share|improve this answer
























                      • the column time is already in datetime format but when the result I get is not correct.

                        – Miku Hatsune
                        Nov 20 '18 at 6:11
















                      -1














                      you can simply use CONVERT(datetime,a.time) in place of CONVERT(date,a.time) in your query.



                      if your requirement something different then could you please send some example data, right now i am not much clear.






                      share|improve this answer
























                      • the column time is already in datetime format but when the result I get is not correct.

                        – Miku Hatsune
                        Nov 20 '18 at 6:11














                      -1












                      -1








                      -1







                      you can simply use CONVERT(datetime,a.time) in place of CONVERT(date,a.time) in your query.



                      if your requirement something different then could you please send some example data, right now i am not much clear.






                      share|improve this answer













                      you can simply use CONVERT(datetime,a.time) in place of CONVERT(date,a.time) in your query.



                      if your requirement something different then could you please send some example data, right now i am not much clear.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 20 '18 at 5:49









                      DevDev

                      194




                      194













                      • the column time is already in datetime format but when the result I get is not correct.

                        – Miku Hatsune
                        Nov 20 '18 at 6:11



















                      • the column time is already in datetime format but when the result I get is not correct.

                        – Miku Hatsune
                        Nov 20 '18 at 6:11

















                      the column time is already in datetime format but when the result I get is not correct.

                      – Miku Hatsune
                      Nov 20 '18 at 6:11





                      the column time is already in datetime format but when the result I get is not correct.

                      – Miku Hatsune
                      Nov 20 '18 at 6:11


















                      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%2f53386732%2fselect-datetime-and-group-by-datetime-query%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

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      Npm cannot find a required file even through it is in the searched directory