Group by with conditions












1














I have to display some data in php the structure of data is like this



id  channel_id  added_by

1 1 9999
2 1 195
3 8 9999
4 1 180
5 1 195
6 8 9999
7 1 9999
8 1 195
9 8 9999


I need to show total count of channel id = 1 only like this



channel_id   total  added_by

1 2 9999
1 4 -1


Its like count of all channel id which are added by 9999 and all other added are assumed as (-1)



I am able to do it like



SELECT channel_id, added_by, sum(id) as total 
from table
where channel_id = 1
group by channel_id ,added_by;


but it gives following result which is not as I need



channel_id  added_by total 
1 9999 2
1 195 3
1 180 1


Please help or give a hint any help is appreciated










share|improve this question





























    1














    I have to display some data in php the structure of data is like this



    id  channel_id  added_by

    1 1 9999
    2 1 195
    3 8 9999
    4 1 180
    5 1 195
    6 8 9999
    7 1 9999
    8 1 195
    9 8 9999


    I need to show total count of channel id = 1 only like this



    channel_id   total  added_by

    1 2 9999
    1 4 -1


    Its like count of all channel id which are added by 9999 and all other added are assumed as (-1)



    I am able to do it like



    SELECT channel_id, added_by, sum(id) as total 
    from table
    where channel_id = 1
    group by channel_id ,added_by;


    but it gives following result which is not as I need



    channel_id  added_by total 
    1 9999 2
    1 195 3
    1 180 1


    Please help or give a hint any help is appreciated










    share|improve this question



























      1












      1








      1







      I have to display some data in php the structure of data is like this



      id  channel_id  added_by

      1 1 9999
      2 1 195
      3 8 9999
      4 1 180
      5 1 195
      6 8 9999
      7 1 9999
      8 1 195
      9 8 9999


      I need to show total count of channel id = 1 only like this



      channel_id   total  added_by

      1 2 9999
      1 4 -1


      Its like count of all channel id which are added by 9999 and all other added are assumed as (-1)



      I am able to do it like



      SELECT channel_id, added_by, sum(id) as total 
      from table
      where channel_id = 1
      group by channel_id ,added_by;


      but it gives following result which is not as I need



      channel_id  added_by total 
      1 9999 2
      1 195 3
      1 180 1


      Please help or give a hint any help is appreciated










      share|improve this question















      I have to display some data in php the structure of data is like this



      id  channel_id  added_by

      1 1 9999
      2 1 195
      3 8 9999
      4 1 180
      5 1 195
      6 8 9999
      7 1 9999
      8 1 195
      9 8 9999


      I need to show total count of channel id = 1 only like this



      channel_id   total  added_by

      1 2 9999
      1 4 -1


      Its like count of all channel id which are added by 9999 and all other added are assumed as (-1)



      I am able to do it like



      SELECT channel_id, added_by, sum(id) as total 
      from table
      where channel_id = 1
      group by channel_id ,added_by;


      but it gives following result which is not as I need



      channel_id  added_by total 
      1 9999 2
      1 195 3
      1 180 1


      Please help or give a hint any help is appreciated







      php mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:15









      Brijesh Kumar Tripathi

      1439




      1439










      asked Nov 12 '18 at 13:01









      user1234user1234

      4,3221724




      4,3221724
























          4 Answers
          4






          active

          oldest

          votes


















          2














          Using CASE..WHEN expressions, you can determine a "modified" added_by value, and GROUP BY on the modified value instead.



          Also, SUM(id) will not give you correct count of rows. It would rather add up the id values. You can use COUNT(*) instead to count the number of rows:



          SELECT channel_id, 
          CASE
          WHEN added_by <> 9999 THEN -1
          ELSE 9999
          END AS modified_added_by,
          COUNT(*) as total
          FROM your_table_name
          WHERE channel_id = 1
          GROUP BY channel_id, modified_added_by


          Result



          | channel_id | modified_added_by | total |
          | ---------- | ----------------- | ----- |
          | 1 | -1 | 4 |
          | 1 | 9999 | 2 |




          View on DB Fiddle






          share|improve this answer































            1














            http://sqlfiddle.com/#!9/437406/2



            SELECT channel_id,
            IF(added_by=9999,9999,-1) new_col,
            COUNT(*)
            FROM `table`
            GROUP BY channel_id, new_col





            share|improve this answer





























              0














              You can try this:



              SELECT channel_id,
              if(9999,9999,-1) as added_by,
              sum(id) as total
              from table
              where channel_id = 1
              group by channel_id,added_by;





              share|improve this answer































                0














                Hope this will help you.



                SELECT count(*) as total, channel_id, added_by from table group by channel_id, added_by;






                share|improve this answer





















                • How it will generate -1 in added_by column.
                  – Brijesh Kumar Tripathi
                  Nov 12 '18 at 13:34











                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%2f53262733%2fgroup-by-with-conditions%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














                Using CASE..WHEN expressions, you can determine a "modified" added_by value, and GROUP BY on the modified value instead.



                Also, SUM(id) will not give you correct count of rows. It would rather add up the id values. You can use COUNT(*) instead to count the number of rows:



                SELECT channel_id, 
                CASE
                WHEN added_by <> 9999 THEN -1
                ELSE 9999
                END AS modified_added_by,
                COUNT(*) as total
                FROM your_table_name
                WHERE channel_id = 1
                GROUP BY channel_id, modified_added_by


                Result



                | channel_id | modified_added_by | total |
                | ---------- | ----------------- | ----- |
                | 1 | -1 | 4 |
                | 1 | 9999 | 2 |




                View on DB Fiddle






                share|improve this answer




























                  2














                  Using CASE..WHEN expressions, you can determine a "modified" added_by value, and GROUP BY on the modified value instead.



                  Also, SUM(id) will not give you correct count of rows. It would rather add up the id values. You can use COUNT(*) instead to count the number of rows:



                  SELECT channel_id, 
                  CASE
                  WHEN added_by <> 9999 THEN -1
                  ELSE 9999
                  END AS modified_added_by,
                  COUNT(*) as total
                  FROM your_table_name
                  WHERE channel_id = 1
                  GROUP BY channel_id, modified_added_by


                  Result



                  | channel_id | modified_added_by | total |
                  | ---------- | ----------------- | ----- |
                  | 1 | -1 | 4 |
                  | 1 | 9999 | 2 |




                  View on DB Fiddle






                  share|improve this answer


























                    2












                    2








                    2






                    Using CASE..WHEN expressions, you can determine a "modified" added_by value, and GROUP BY on the modified value instead.



                    Also, SUM(id) will not give you correct count of rows. It would rather add up the id values. You can use COUNT(*) instead to count the number of rows:



                    SELECT channel_id, 
                    CASE
                    WHEN added_by <> 9999 THEN -1
                    ELSE 9999
                    END AS modified_added_by,
                    COUNT(*) as total
                    FROM your_table_name
                    WHERE channel_id = 1
                    GROUP BY channel_id, modified_added_by


                    Result



                    | channel_id | modified_added_by | total |
                    | ---------- | ----------------- | ----- |
                    | 1 | -1 | 4 |
                    | 1 | 9999 | 2 |




                    View on DB Fiddle






                    share|improve this answer














                    Using CASE..WHEN expressions, you can determine a "modified" added_by value, and GROUP BY on the modified value instead.



                    Also, SUM(id) will not give you correct count of rows. It would rather add up the id values. You can use COUNT(*) instead to count the number of rows:



                    SELECT channel_id, 
                    CASE
                    WHEN added_by <> 9999 THEN -1
                    ELSE 9999
                    END AS modified_added_by,
                    COUNT(*) as total
                    FROM your_table_name
                    WHERE channel_id = 1
                    GROUP BY channel_id, modified_added_by


                    Result



                    | channel_id | modified_added_by | total |
                    | ---------- | ----------------- | ----- |
                    | 1 | -1 | 4 |
                    | 1 | 9999 | 2 |




                    View on DB Fiddle







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 12 '18 at 20:48

























                    answered Nov 12 '18 at 13:03









                    Madhur BhaiyaMadhur Bhaiya

                    19.5k62236




                    19.5k62236

























                        1














                        http://sqlfiddle.com/#!9/437406/2



                        SELECT channel_id,
                        IF(added_by=9999,9999,-1) new_col,
                        COUNT(*)
                        FROM `table`
                        GROUP BY channel_id, new_col





                        share|improve this answer


























                          1














                          http://sqlfiddle.com/#!9/437406/2



                          SELECT channel_id,
                          IF(added_by=9999,9999,-1) new_col,
                          COUNT(*)
                          FROM `table`
                          GROUP BY channel_id, new_col





                          share|improve this answer
























                            1












                            1








                            1






                            http://sqlfiddle.com/#!9/437406/2



                            SELECT channel_id,
                            IF(added_by=9999,9999,-1) new_col,
                            COUNT(*)
                            FROM `table`
                            GROUP BY channel_id, new_col





                            share|improve this answer












                            http://sqlfiddle.com/#!9/437406/2



                            SELECT channel_id,
                            IF(added_by=9999,9999,-1) new_col,
                            COUNT(*)
                            FROM `table`
                            GROUP BY channel_id, new_col






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 '18 at 14:14









                            AlexAlex

                            14.3k11836




                            14.3k11836























                                0














                                You can try this:



                                SELECT channel_id,
                                if(9999,9999,-1) as added_by,
                                sum(id) as total
                                from table
                                where channel_id = 1
                                group by channel_id,added_by;





                                share|improve this answer




























                                  0














                                  You can try this:



                                  SELECT channel_id,
                                  if(9999,9999,-1) as added_by,
                                  sum(id) as total
                                  from table
                                  where channel_id = 1
                                  group by channel_id,added_by;





                                  share|improve this answer


























                                    0












                                    0








                                    0






                                    You can try this:



                                    SELECT channel_id,
                                    if(9999,9999,-1) as added_by,
                                    sum(id) as total
                                    from table
                                    where channel_id = 1
                                    group by channel_id,added_by;





                                    share|improve this answer














                                    You can try this:



                                    SELECT channel_id,
                                    if(9999,9999,-1) as added_by,
                                    sum(id) as total
                                    from table
                                    where channel_id = 1
                                    group by channel_id,added_by;






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 12 '18 at 13:21









                                    Francesco Boi

                                    1,58421935




                                    1,58421935










                                    answered Nov 12 '18 at 13:08









                                    user2485245user2485245

                                    11




                                    11























                                        0














                                        Hope this will help you.



                                        SELECT count(*) as total, channel_id, added_by from table group by channel_id, added_by;






                                        share|improve this answer





















                                        • How it will generate -1 in added_by column.
                                          – Brijesh Kumar Tripathi
                                          Nov 12 '18 at 13:34
















                                        0














                                        Hope this will help you.



                                        SELECT count(*) as total, channel_id, added_by from table group by channel_id, added_by;






                                        share|improve this answer





















                                        • How it will generate -1 in added_by column.
                                          – Brijesh Kumar Tripathi
                                          Nov 12 '18 at 13:34














                                        0












                                        0








                                        0






                                        Hope this will help you.



                                        SELECT count(*) as total, channel_id, added_by from table group by channel_id, added_by;






                                        share|improve this answer












                                        Hope this will help you.



                                        SELECT count(*) as total, channel_id, added_by from table group by channel_id, added_by;







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 12 '18 at 13:21









                                        Dharmik RavalDharmik Raval

                                        15




                                        15












                                        • How it will generate -1 in added_by column.
                                          – Brijesh Kumar Tripathi
                                          Nov 12 '18 at 13:34


















                                        • How it will generate -1 in added_by column.
                                          – Brijesh Kumar Tripathi
                                          Nov 12 '18 at 13:34
















                                        How it will generate -1 in added_by column.
                                        – Brijesh Kumar Tripathi
                                        Nov 12 '18 at 13:34




                                        How it will generate -1 in added_by column.
                                        – Brijesh Kumar Tripathi
                                        Nov 12 '18 at 13:34


















                                        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%2f53262733%2fgroup-by-with-conditions%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

                                        Full-time equivalent

                                        さくらももこ

                                        13 indicted, 8 arrested in Calif. drug cartel investigation