SQL Query - COUNT for combination of two or more column from single table












3















I have a table with below column & row values and want a resultset as given below. I tried many queries but could not getthe resultset which I want.
Is there any simplest way to achieve this.



Column1 Column2  Column3 Column4 Column5
AAA 000000 BG1 12345 North
AAA 111111 BG2 23456 South
BBB 000000 BG3 12346 EAST
AAA 000000 BG2 12345 West

Select Column1,Count( Distinct Column1,Column2),Count(Distinct Column1,Column3),Count(Column1,Column4,Column5) From #Temp


Expected Resultset:



Name C2Count C3Count C4Count
AAA 2 2 3
BBB 1 1 1









share|improve this question



























    3















    I have a table with below column & row values and want a resultset as given below. I tried many queries but could not getthe resultset which I want.
    Is there any simplest way to achieve this.



    Column1 Column2  Column3 Column4 Column5
    AAA 000000 BG1 12345 North
    AAA 111111 BG2 23456 South
    BBB 000000 BG3 12346 EAST
    AAA 000000 BG2 12345 West

    Select Column1,Count( Distinct Column1,Column2),Count(Distinct Column1,Column3),Count(Column1,Column4,Column5) From #Temp


    Expected Resultset:



    Name C2Count C3Count C4Count
    AAA 2 2 3
    BBB 1 1 1









    share|improve this question

























      3












      3








      3








      I have a table with below column & row values and want a resultset as given below. I tried many queries but could not getthe resultset which I want.
      Is there any simplest way to achieve this.



      Column1 Column2  Column3 Column4 Column5
      AAA 000000 BG1 12345 North
      AAA 111111 BG2 23456 South
      BBB 000000 BG3 12346 EAST
      AAA 000000 BG2 12345 West

      Select Column1,Count( Distinct Column1,Column2),Count(Distinct Column1,Column3),Count(Column1,Column4,Column5) From #Temp


      Expected Resultset:



      Name C2Count C3Count C4Count
      AAA 2 2 3
      BBB 1 1 1









      share|improve this question














      I have a table with below column & row values and want a resultset as given below. I tried many queries but could not getthe resultset which I want.
      Is there any simplest way to achieve this.



      Column1 Column2  Column3 Column4 Column5
      AAA 000000 BG1 12345 North
      AAA 111111 BG2 23456 South
      BBB 000000 BG3 12346 EAST
      AAA 000000 BG2 12345 West

      Select Column1,Count( Distinct Column1,Column2),Count(Distinct Column1,Column3),Count(Column1,Column4,Column5) From #Temp


      Expected Resultset:



      Name C2Count C3Count C4Count
      AAA 2 2 3
      BBB 1 1 1






      sql-server sql-server-2008 sql-server-2012 sql-server-2008-r2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 6:36









      Ask_SOAsk_SO

      407315




      407315
























          3 Answers
          3






          active

          oldest

          votes


















          0














          I don't see anyway to avoid doing two separate aggregations. One can handle the single distinct counts while the other can handle the two column count:



          WITH cte1 AS (
          SELECT
          Column1 AS Name,
          COUNT(DISTINCT Column2) AS C2Count,
          COUNT(DISTINCT Column3) AS C3Count
          FROM yourTable
          GROUP BY Column1
          ),
          cte2 AS (
          SELECT Name, COUNT(*) AS C45Count
          FROM
          (
          SELECT DISTINCT Column1 AS Name, Column4, Column5 FROM yourTable
          ) t
          GROUP BY Name
          )

          SELECT
          t1.Name,
          t1.C2Count,
          t1.C3Count,
          t2.C45Count
          FROM cte1 t1
          INNER JOIN cte2 t2
          ON t1.Name = t2.Name;



          Demo






          share|improve this answer


























          • I want count from combination of two or three columns for c4 count. I could not get this. Please let me know if there is any simplest way.

            – Ask_SO
            Jan 19 at 6:48











          • @Ask_SO I fixed my answer.

            – Tim Biegeleisen
            Jan 19 at 8:38



















          0














          Perhaps you're looking for this ? :



          DECLARE 
          @tb TABLE
          (
          Column1 VARCHAR(50)
          , Column2 VARCHAR(50)
          , Column3 VARCHAR(50)
          , Column4 INT
          , Column5 VARCHAR(50)
          )
          INSERT INTO @tb VALUES
          ('AAA','000000','BG1',12345,'North'),
          ('AAA','111111','BG2',23456,'South'),
          ('BBB','000000','BG3',12346,'EAST'),
          ('AAA','000000','BG2',12345,'West')

          SELECT
          Column1 [Name]
          , COUNT(DISTINCT Column2) C2Count
          , COUNT(DISTINCT Column3) C3Count
          , SUM(C45Count) C4Count
          FROM (
          SELECT *, COUNT(Column1) OVER(PARTITION BY Column1, Column4, Column5) C45Count
          FROM @tb
          ) D
          GROUP BY
          Column1





          share|improve this answer


























          • This coincidentally happens to generate the correct output, but the OP seems to want the distinct count of columns 4 and 5.

            – Tim Biegeleisen
            Jan 19 at 8:39











          • @TimBiegeleisen If we group by Column1, and count, isn't going to give us the same count as counting column1 ? not sure, maybe I'm missing something out. I've also updated the query to fit OP request.

            – iSR5
            Jan 19 at 9:14



















          0














          I am a little confused on how you are wanting the C4count calculated. Using this data(thanks @iSR5):



          CREATE TABLE #Table
          (
          Column1 VARCHAR(50)
          , Column2 VARCHAR(50)
          , Column3 VARCHAR(50)
          , Column4 INT
          , Column5 VARCHAR(50)
          )
          INSERT INTO #Table VALUES
          ('AAA','000000','BG1',12345,'North'),
          ('AAA','111111','BG2',23456,'South'),
          ('BBB','000000','BG3',12346,'EAST'),
          ('AAA','000000','BG2',12345,'West')


          You can use a pretty basic Statement to achieve the desired results:



          SELECT   Column1 AS [Name]
          ,COUNT(DISTINCT t.Column2) AS C2Count
          ,COUNT(DISTINCT t.Column3) AS C3Count
          ,COUNT(t.Column4) AS C4Count
          FROM #Table t
          GROUP BY t.Column1


          Results:



          Name C2Count C3Count C4Count
          AAA 2 2 3
          BBB 1 1 1


          This will count how many distinct matches are in each column for AAA and BBB for column2 and column 3, and count overall matches for column4. Your desired results don't show anything for column5.



          If there is some other sort of calculation you are wanting, could you please clearly describe what you are looking for so I can adjust my code to show what it is you are looking for.






          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%2f54264687%2fsql-query-count-for-combination-of-two-or-more-column-from-single-table%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














            I don't see anyway to avoid doing two separate aggregations. One can handle the single distinct counts while the other can handle the two column count:



            WITH cte1 AS (
            SELECT
            Column1 AS Name,
            COUNT(DISTINCT Column2) AS C2Count,
            COUNT(DISTINCT Column3) AS C3Count
            FROM yourTable
            GROUP BY Column1
            ),
            cte2 AS (
            SELECT Name, COUNT(*) AS C45Count
            FROM
            (
            SELECT DISTINCT Column1 AS Name, Column4, Column5 FROM yourTable
            ) t
            GROUP BY Name
            )

            SELECT
            t1.Name,
            t1.C2Count,
            t1.C3Count,
            t2.C45Count
            FROM cte1 t1
            INNER JOIN cte2 t2
            ON t1.Name = t2.Name;



            Demo






            share|improve this answer


























            • I want count from combination of two or three columns for c4 count. I could not get this. Please let me know if there is any simplest way.

              – Ask_SO
              Jan 19 at 6:48











            • @Ask_SO I fixed my answer.

              – Tim Biegeleisen
              Jan 19 at 8:38
















            0














            I don't see anyway to avoid doing two separate aggregations. One can handle the single distinct counts while the other can handle the two column count:



            WITH cte1 AS (
            SELECT
            Column1 AS Name,
            COUNT(DISTINCT Column2) AS C2Count,
            COUNT(DISTINCT Column3) AS C3Count
            FROM yourTable
            GROUP BY Column1
            ),
            cte2 AS (
            SELECT Name, COUNT(*) AS C45Count
            FROM
            (
            SELECT DISTINCT Column1 AS Name, Column4, Column5 FROM yourTable
            ) t
            GROUP BY Name
            )

            SELECT
            t1.Name,
            t1.C2Count,
            t1.C3Count,
            t2.C45Count
            FROM cte1 t1
            INNER JOIN cte2 t2
            ON t1.Name = t2.Name;



            Demo






            share|improve this answer


























            • I want count from combination of two or three columns for c4 count. I could not get this. Please let me know if there is any simplest way.

              – Ask_SO
              Jan 19 at 6:48











            • @Ask_SO I fixed my answer.

              – Tim Biegeleisen
              Jan 19 at 8:38














            0












            0








            0







            I don't see anyway to avoid doing two separate aggregations. One can handle the single distinct counts while the other can handle the two column count:



            WITH cte1 AS (
            SELECT
            Column1 AS Name,
            COUNT(DISTINCT Column2) AS C2Count,
            COUNT(DISTINCT Column3) AS C3Count
            FROM yourTable
            GROUP BY Column1
            ),
            cte2 AS (
            SELECT Name, COUNT(*) AS C45Count
            FROM
            (
            SELECT DISTINCT Column1 AS Name, Column4, Column5 FROM yourTable
            ) t
            GROUP BY Name
            )

            SELECT
            t1.Name,
            t1.C2Count,
            t1.C3Count,
            t2.C45Count
            FROM cte1 t1
            INNER JOIN cte2 t2
            ON t1.Name = t2.Name;



            Demo






            share|improve this answer















            I don't see anyway to avoid doing two separate aggregations. One can handle the single distinct counts while the other can handle the two column count:



            WITH cte1 AS (
            SELECT
            Column1 AS Name,
            COUNT(DISTINCT Column2) AS C2Count,
            COUNT(DISTINCT Column3) AS C3Count
            FROM yourTable
            GROUP BY Column1
            ),
            cte2 AS (
            SELECT Name, COUNT(*) AS C45Count
            FROM
            (
            SELECT DISTINCT Column1 AS Name, Column4, Column5 FROM yourTable
            ) t
            GROUP BY Name
            )

            SELECT
            t1.Name,
            t1.C2Count,
            t1.C3Count,
            t2.C45Count
            FROM cte1 t1
            INNER JOIN cte2 t2
            ON t1.Name = t2.Name;



            Demo







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 19 at 8:36

























            answered Jan 19 at 6:46









            Tim BiegeleisenTim Biegeleisen

            223k1391143




            223k1391143













            • I want count from combination of two or three columns for c4 count. I could not get this. Please let me know if there is any simplest way.

              – Ask_SO
              Jan 19 at 6:48











            • @Ask_SO I fixed my answer.

              – Tim Biegeleisen
              Jan 19 at 8:38



















            • I want count from combination of two or three columns for c4 count. I could not get this. Please let me know if there is any simplest way.

              – Ask_SO
              Jan 19 at 6:48











            • @Ask_SO I fixed my answer.

              – Tim Biegeleisen
              Jan 19 at 8:38

















            I want count from combination of two or three columns for c4 count. I could not get this. Please let me know if there is any simplest way.

            – Ask_SO
            Jan 19 at 6:48





            I want count from combination of two or three columns for c4 count. I could not get this. Please let me know if there is any simplest way.

            – Ask_SO
            Jan 19 at 6:48













            @Ask_SO I fixed my answer.

            – Tim Biegeleisen
            Jan 19 at 8:38





            @Ask_SO I fixed my answer.

            – Tim Biegeleisen
            Jan 19 at 8:38













            0














            Perhaps you're looking for this ? :



            DECLARE 
            @tb TABLE
            (
            Column1 VARCHAR(50)
            , Column2 VARCHAR(50)
            , Column3 VARCHAR(50)
            , Column4 INT
            , Column5 VARCHAR(50)
            )
            INSERT INTO @tb VALUES
            ('AAA','000000','BG1',12345,'North'),
            ('AAA','111111','BG2',23456,'South'),
            ('BBB','000000','BG3',12346,'EAST'),
            ('AAA','000000','BG2',12345,'West')

            SELECT
            Column1 [Name]
            , COUNT(DISTINCT Column2) C2Count
            , COUNT(DISTINCT Column3) C3Count
            , SUM(C45Count) C4Count
            FROM (
            SELECT *, COUNT(Column1) OVER(PARTITION BY Column1, Column4, Column5) C45Count
            FROM @tb
            ) D
            GROUP BY
            Column1





            share|improve this answer


























            • This coincidentally happens to generate the correct output, but the OP seems to want the distinct count of columns 4 and 5.

              – Tim Biegeleisen
              Jan 19 at 8:39











            • @TimBiegeleisen If we group by Column1, and count, isn't going to give us the same count as counting column1 ? not sure, maybe I'm missing something out. I've also updated the query to fit OP request.

              – iSR5
              Jan 19 at 9:14
















            0














            Perhaps you're looking for this ? :



            DECLARE 
            @tb TABLE
            (
            Column1 VARCHAR(50)
            , Column2 VARCHAR(50)
            , Column3 VARCHAR(50)
            , Column4 INT
            , Column5 VARCHAR(50)
            )
            INSERT INTO @tb VALUES
            ('AAA','000000','BG1',12345,'North'),
            ('AAA','111111','BG2',23456,'South'),
            ('BBB','000000','BG3',12346,'EAST'),
            ('AAA','000000','BG2',12345,'West')

            SELECT
            Column1 [Name]
            , COUNT(DISTINCT Column2) C2Count
            , COUNT(DISTINCT Column3) C3Count
            , SUM(C45Count) C4Count
            FROM (
            SELECT *, COUNT(Column1) OVER(PARTITION BY Column1, Column4, Column5) C45Count
            FROM @tb
            ) D
            GROUP BY
            Column1





            share|improve this answer


























            • This coincidentally happens to generate the correct output, but the OP seems to want the distinct count of columns 4 and 5.

              – Tim Biegeleisen
              Jan 19 at 8:39











            • @TimBiegeleisen If we group by Column1, and count, isn't going to give us the same count as counting column1 ? not sure, maybe I'm missing something out. I've also updated the query to fit OP request.

              – iSR5
              Jan 19 at 9:14














            0












            0








            0







            Perhaps you're looking for this ? :



            DECLARE 
            @tb TABLE
            (
            Column1 VARCHAR(50)
            , Column2 VARCHAR(50)
            , Column3 VARCHAR(50)
            , Column4 INT
            , Column5 VARCHAR(50)
            )
            INSERT INTO @tb VALUES
            ('AAA','000000','BG1',12345,'North'),
            ('AAA','111111','BG2',23456,'South'),
            ('BBB','000000','BG3',12346,'EAST'),
            ('AAA','000000','BG2',12345,'West')

            SELECT
            Column1 [Name]
            , COUNT(DISTINCT Column2) C2Count
            , COUNT(DISTINCT Column3) C3Count
            , SUM(C45Count) C4Count
            FROM (
            SELECT *, COUNT(Column1) OVER(PARTITION BY Column1, Column4, Column5) C45Count
            FROM @tb
            ) D
            GROUP BY
            Column1





            share|improve this answer















            Perhaps you're looking for this ? :



            DECLARE 
            @tb TABLE
            (
            Column1 VARCHAR(50)
            , Column2 VARCHAR(50)
            , Column3 VARCHAR(50)
            , Column4 INT
            , Column5 VARCHAR(50)
            )
            INSERT INTO @tb VALUES
            ('AAA','000000','BG1',12345,'North'),
            ('AAA','111111','BG2',23456,'South'),
            ('BBB','000000','BG3',12346,'EAST'),
            ('AAA','000000','BG2',12345,'West')

            SELECT
            Column1 [Name]
            , COUNT(DISTINCT Column2) C2Count
            , COUNT(DISTINCT Column3) C3Count
            , SUM(C45Count) C4Count
            FROM (
            SELECT *, COUNT(Column1) OVER(PARTITION BY Column1, Column4, Column5) C45Count
            FROM @tb
            ) D
            GROUP BY
            Column1






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 19 at 9:11

























            answered Jan 19 at 7:17









            iSR5iSR5

            1,488278




            1,488278













            • This coincidentally happens to generate the correct output, but the OP seems to want the distinct count of columns 4 and 5.

              – Tim Biegeleisen
              Jan 19 at 8:39











            • @TimBiegeleisen If we group by Column1, and count, isn't going to give us the same count as counting column1 ? not sure, maybe I'm missing something out. I've also updated the query to fit OP request.

              – iSR5
              Jan 19 at 9:14



















            • This coincidentally happens to generate the correct output, but the OP seems to want the distinct count of columns 4 and 5.

              – Tim Biegeleisen
              Jan 19 at 8:39











            • @TimBiegeleisen If we group by Column1, and count, isn't going to give us the same count as counting column1 ? not sure, maybe I'm missing something out. I've also updated the query to fit OP request.

              – iSR5
              Jan 19 at 9:14

















            This coincidentally happens to generate the correct output, but the OP seems to want the distinct count of columns 4 and 5.

            – Tim Biegeleisen
            Jan 19 at 8:39





            This coincidentally happens to generate the correct output, but the OP seems to want the distinct count of columns 4 and 5.

            – Tim Biegeleisen
            Jan 19 at 8:39













            @TimBiegeleisen If we group by Column1, and count, isn't going to give us the same count as counting column1 ? not sure, maybe I'm missing something out. I've also updated the query to fit OP request.

            – iSR5
            Jan 19 at 9:14





            @TimBiegeleisen If we group by Column1, and count, isn't going to give us the same count as counting column1 ? not sure, maybe I'm missing something out. I've also updated the query to fit OP request.

            – iSR5
            Jan 19 at 9:14











            0














            I am a little confused on how you are wanting the C4count calculated. Using this data(thanks @iSR5):



            CREATE TABLE #Table
            (
            Column1 VARCHAR(50)
            , Column2 VARCHAR(50)
            , Column3 VARCHAR(50)
            , Column4 INT
            , Column5 VARCHAR(50)
            )
            INSERT INTO #Table VALUES
            ('AAA','000000','BG1',12345,'North'),
            ('AAA','111111','BG2',23456,'South'),
            ('BBB','000000','BG3',12346,'EAST'),
            ('AAA','000000','BG2',12345,'West')


            You can use a pretty basic Statement to achieve the desired results:



            SELECT   Column1 AS [Name]
            ,COUNT(DISTINCT t.Column2) AS C2Count
            ,COUNT(DISTINCT t.Column3) AS C3Count
            ,COUNT(t.Column4) AS C4Count
            FROM #Table t
            GROUP BY t.Column1


            Results:



            Name C2Count C3Count C4Count
            AAA 2 2 3
            BBB 1 1 1


            This will count how many distinct matches are in each column for AAA and BBB for column2 and column 3, and count overall matches for column4. Your desired results don't show anything for column5.



            If there is some other sort of calculation you are wanting, could you please clearly describe what you are looking for so I can adjust my code to show what it is you are looking for.






            share|improve this answer




























              0














              I am a little confused on how you are wanting the C4count calculated. Using this data(thanks @iSR5):



              CREATE TABLE #Table
              (
              Column1 VARCHAR(50)
              , Column2 VARCHAR(50)
              , Column3 VARCHAR(50)
              , Column4 INT
              , Column5 VARCHAR(50)
              )
              INSERT INTO #Table VALUES
              ('AAA','000000','BG1',12345,'North'),
              ('AAA','111111','BG2',23456,'South'),
              ('BBB','000000','BG3',12346,'EAST'),
              ('AAA','000000','BG2',12345,'West')


              You can use a pretty basic Statement to achieve the desired results:



              SELECT   Column1 AS [Name]
              ,COUNT(DISTINCT t.Column2) AS C2Count
              ,COUNT(DISTINCT t.Column3) AS C3Count
              ,COUNT(t.Column4) AS C4Count
              FROM #Table t
              GROUP BY t.Column1


              Results:



              Name C2Count C3Count C4Count
              AAA 2 2 3
              BBB 1 1 1


              This will count how many distinct matches are in each column for AAA and BBB for column2 and column 3, and count overall matches for column4. Your desired results don't show anything for column5.



              If there is some other sort of calculation you are wanting, could you please clearly describe what you are looking for so I can adjust my code to show what it is you are looking for.






              share|improve this answer


























                0












                0








                0







                I am a little confused on how you are wanting the C4count calculated. Using this data(thanks @iSR5):



                CREATE TABLE #Table
                (
                Column1 VARCHAR(50)
                , Column2 VARCHAR(50)
                , Column3 VARCHAR(50)
                , Column4 INT
                , Column5 VARCHAR(50)
                )
                INSERT INTO #Table VALUES
                ('AAA','000000','BG1',12345,'North'),
                ('AAA','111111','BG2',23456,'South'),
                ('BBB','000000','BG3',12346,'EAST'),
                ('AAA','000000','BG2',12345,'West')


                You can use a pretty basic Statement to achieve the desired results:



                SELECT   Column1 AS [Name]
                ,COUNT(DISTINCT t.Column2) AS C2Count
                ,COUNT(DISTINCT t.Column3) AS C3Count
                ,COUNT(t.Column4) AS C4Count
                FROM #Table t
                GROUP BY t.Column1


                Results:



                Name C2Count C3Count C4Count
                AAA 2 2 3
                BBB 1 1 1


                This will count how many distinct matches are in each column for AAA and BBB for column2 and column 3, and count overall matches for column4. Your desired results don't show anything for column5.



                If there is some other sort of calculation you are wanting, could you please clearly describe what you are looking for so I can adjust my code to show what it is you are looking for.






                share|improve this answer













                I am a little confused on how you are wanting the C4count calculated. Using this data(thanks @iSR5):



                CREATE TABLE #Table
                (
                Column1 VARCHAR(50)
                , Column2 VARCHAR(50)
                , Column3 VARCHAR(50)
                , Column4 INT
                , Column5 VARCHAR(50)
                )
                INSERT INTO #Table VALUES
                ('AAA','000000','BG1',12345,'North'),
                ('AAA','111111','BG2',23456,'South'),
                ('BBB','000000','BG3',12346,'EAST'),
                ('AAA','000000','BG2',12345,'West')


                You can use a pretty basic Statement to achieve the desired results:



                SELECT   Column1 AS [Name]
                ,COUNT(DISTINCT t.Column2) AS C2Count
                ,COUNT(DISTINCT t.Column3) AS C3Count
                ,COUNT(t.Column4) AS C4Count
                FROM #Table t
                GROUP BY t.Column1


                Results:



                Name C2Count C3Count C4Count
                AAA 2 2 3
                BBB 1 1 1


                This will count how many distinct matches are in each column for AAA and BBB for column2 and column 3, and count overall matches for column4. Your desired results don't show anything for column5.



                If there is some other sort of calculation you are wanting, could you please clearly describe what you are looking for so I can adjust my code to show what it is you are looking for.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 22 at 23:12









                Nick ANick A

                797




                797






























                    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%2f54264687%2fsql-query-count-for-combination-of-two-or-more-column-from-single-table%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

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage