SQL Use ID to count dates but only can count each once












1














TABLE 1



  ID   DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018


So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.



For example my expected output is



  1/1/2018 .  1
1/2/2018 . 2
1/3/2018 . 1


But the output i'm getting is



  1/1/2018 .  1
1/2/2018 . 3
1/3/2018 . 1









share|improve this question
























  • You should post your query in question as well in order to rectify issue.
    – Yogesh Sharma
    Nov 19 '18 at 17:38










  • So the row 4 . 1/2/2018 does not count in your expected output?
    – forpas
    Nov 19 '18 at 18:02










  • It does count in my output but I don't want it to count one of the dates with the id of 2.
    – Brandon Brown
    Nov 19 '18 at 18:05










  • So there is an extra row 1/2/2018 . 1 in your expected output?
    – forpas
    Nov 19 '18 at 18:07










  • yes an extra one is being counted. Yogesh solved it. Thank you.
    – Brandon Brown
    Nov 19 '18 at 18:10
















1














TABLE 1



  ID   DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018


So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.



For example my expected output is



  1/1/2018 .  1
1/2/2018 . 2
1/3/2018 . 1


But the output i'm getting is



  1/1/2018 .  1
1/2/2018 . 3
1/3/2018 . 1









share|improve this question
























  • You should post your query in question as well in order to rectify issue.
    – Yogesh Sharma
    Nov 19 '18 at 17:38










  • So the row 4 . 1/2/2018 does not count in your expected output?
    – forpas
    Nov 19 '18 at 18:02










  • It does count in my output but I don't want it to count one of the dates with the id of 2.
    – Brandon Brown
    Nov 19 '18 at 18:05










  • So there is an extra row 1/2/2018 . 1 in your expected output?
    – forpas
    Nov 19 '18 at 18:07










  • yes an extra one is being counted. Yogesh solved it. Thank you.
    – Brandon Brown
    Nov 19 '18 at 18:10














1












1








1







TABLE 1



  ID   DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018


So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.



For example my expected output is



  1/1/2018 .  1
1/2/2018 . 2
1/3/2018 . 1


But the output i'm getting is



  1/1/2018 .  1
1/2/2018 . 3
1/3/2018 . 1









share|improve this question















TABLE 1



  ID   DATE
1 . 1/1/2018
2 . 1/2/2018
2 . 1/2/2018
3 . 1/3/2018
4 . 1/2/2018


So I need to get a count of each date but there are cases where the same ID has the same date. I need to only count that one once.



For example my expected output is



  1/1/2018 .  1
1/2/2018 . 2
1/3/2018 . 1


But the output i'm getting is



  1/1/2018 .  1
1/2/2018 . 3
1/3/2018 . 1






sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:39









Yogesh Sharma

28.3k51335




28.3k51335










asked Nov 19 '18 at 17:33









Brandon BrownBrandon Brown

144




144












  • You should post your query in question as well in order to rectify issue.
    – Yogesh Sharma
    Nov 19 '18 at 17:38










  • So the row 4 . 1/2/2018 does not count in your expected output?
    – forpas
    Nov 19 '18 at 18:02










  • It does count in my output but I don't want it to count one of the dates with the id of 2.
    – Brandon Brown
    Nov 19 '18 at 18:05










  • So there is an extra row 1/2/2018 . 1 in your expected output?
    – forpas
    Nov 19 '18 at 18:07










  • yes an extra one is being counted. Yogesh solved it. Thank you.
    – Brandon Brown
    Nov 19 '18 at 18:10


















  • You should post your query in question as well in order to rectify issue.
    – Yogesh Sharma
    Nov 19 '18 at 17:38










  • So the row 4 . 1/2/2018 does not count in your expected output?
    – forpas
    Nov 19 '18 at 18:02










  • It does count in my output but I don't want it to count one of the dates with the id of 2.
    – Brandon Brown
    Nov 19 '18 at 18:05










  • So there is an extra row 1/2/2018 . 1 in your expected output?
    – forpas
    Nov 19 '18 at 18:07










  • yes an extra one is being counted. Yogesh solved it. Thank you.
    – Brandon Brown
    Nov 19 '18 at 18:10
















You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38




You should post your query in question as well in order to rectify issue.
– Yogesh Sharma
Nov 19 '18 at 17:38












So the row 4 . 1/2/2018 does not count in your expected output?
– forpas
Nov 19 '18 at 18:02




So the row 4 . 1/2/2018 does not count in your expected output?
– forpas
Nov 19 '18 at 18:02












It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05




It does count in my output but I don't want it to count one of the dates with the id of 2.
– Brandon Brown
Nov 19 '18 at 18:05












So there is an extra row 1/2/2018 . 1 in your expected output?
– forpas
Nov 19 '18 at 18:07




So there is an extra row 1/2/2018 . 1 in your expected output?
– forpas
Nov 19 '18 at 18:07












yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10




yes an extra one is being counted. Yogesh solved it. Thank you.
– Brandon Brown
Nov 19 '18 at 18:10












3 Answers
3






active

oldest

votes


















3














You need DISTINCT inside COUNT() to considered same id as once :



SELECT DATE, COUNT(DISTINCT ID)
FROM table1 t
GROUP BY DATE;





share|improve this answer





























    0














    You need to GROUP by ID and DATE:



    SELECT ID, DATE, COUNT(DATE) AS COUNTER
    FROM table1
    GROUP BY ID, DATE





    share|improve this answer





























      0














      You could use a subquery where you concatenate the key and the date using distinct.



      SELECT DATE, COUNT(DATE) FROM table1 
      where DATE + ID =
      (select DATE + ID from table1)
      group by DATE





      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%2f53379889%2fsql-use-id-to-count-dates-but-only-can-count-each-once%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









        3














        You need DISTINCT inside COUNT() to considered same id as once :



        SELECT DATE, COUNT(DISTINCT ID)
        FROM table1 t
        GROUP BY DATE;





        share|improve this answer


























          3














          You need DISTINCT inside COUNT() to considered same id as once :



          SELECT DATE, COUNT(DISTINCT ID)
          FROM table1 t
          GROUP BY DATE;





          share|improve this answer
























            3












            3








            3






            You need DISTINCT inside COUNT() to considered same id as once :



            SELECT DATE, COUNT(DISTINCT ID)
            FROM table1 t
            GROUP BY DATE;





            share|improve this answer












            You need DISTINCT inside COUNT() to considered same id as once :



            SELECT DATE, COUNT(DISTINCT ID)
            FROM table1 t
            GROUP BY DATE;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 17:35









            Yogesh SharmaYogesh Sharma

            28.3k51335




            28.3k51335

























                0














                You need to GROUP by ID and DATE:



                SELECT ID, DATE, COUNT(DATE) AS COUNTER
                FROM table1
                GROUP BY ID, DATE





                share|improve this answer


























                  0














                  You need to GROUP by ID and DATE:



                  SELECT ID, DATE, COUNT(DATE) AS COUNTER
                  FROM table1
                  GROUP BY ID, DATE





                  share|improve this answer
























                    0












                    0








                    0






                    You need to GROUP by ID and DATE:



                    SELECT ID, DATE, COUNT(DATE) AS COUNTER
                    FROM table1
                    GROUP BY ID, DATE





                    share|improve this answer












                    You need to GROUP by ID and DATE:



                    SELECT ID, DATE, COUNT(DATE) AS COUNTER
                    FROM table1
                    GROUP BY ID, DATE






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 '18 at 18:28









                    forpasforpas

                    9,3091421




                    9,3091421























                        0














                        You could use a subquery where you concatenate the key and the date using distinct.



                        SELECT DATE, COUNT(DATE) FROM table1 
                        where DATE + ID =
                        (select DATE + ID from table1)
                        group by DATE





                        share|improve this answer


























                          0














                          You could use a subquery where you concatenate the key and the date using distinct.



                          SELECT DATE, COUNT(DATE) FROM table1 
                          where DATE + ID =
                          (select DATE + ID from table1)
                          group by DATE





                          share|improve this answer
























                            0












                            0








                            0






                            You could use a subquery where you concatenate the key and the date using distinct.



                            SELECT DATE, COUNT(DATE) FROM table1 
                            where DATE + ID =
                            (select DATE + ID from table1)
                            group by DATE





                            share|improve this answer












                            You could use a subquery where you concatenate the key and the date using distinct.



                            SELECT DATE, COUNT(DATE) FROM table1 
                            where DATE + ID =
                            (select DATE + ID from table1)
                            group by DATE






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 19 '18 at 18:56









                            KarlomanioKarlomanio

                            27528




                            27528






























                                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.





                                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%2fstackoverflow.com%2fquestions%2f53379889%2fsql-use-id-to-count-dates-but-only-can-count-each-once%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

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

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