Query: I have 4 rows, need to add the results from 3 rows into one, and leave the last row untouched










1















I have a kind of tricky question for this query. First the code:



SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
FROM incident
INNER JOIN user ON incident.user_id=user.user_id
INNER JOIN user_type ON user.user_type=user_type.user_type
WHERE incident.code=2
GROUP BY user.user_type


What Am I doing?



For example, I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2).



this brings the following result:



+-----------------------+----------+
| user_type_description | Quantity |
+-----------------------+----------+
| Admin | 6 |
| Moderator | 8 |
| Fully_registered_user | 8 |
| anonymous_user | 9 |
+-----------------------+----------+


Basically Admin,Moderator and Fully_registered_user are appropriately registered users. I need to add them in a result where it shows like:



+--------------+------------+
| Proper_users | Anonymous |
+--------------+------------+
| 22 | 9 |
+--------------+------------+


I am not good with sql. Any help is appreciated. Thanks.










share|improve this question



















  • 1





    Show your sample data and expected result with formatted table.. You can use This And your group by is weird.. Your select use user_type.user_type_description but your group by user.user_type

    – dwir182
    Nov 13 '18 at 0:09












  • I used your link, but stackoverflow just omits spaces. Yes, I group by user type, which is a number. 0=admin 1=mod 2=fully_reg_user 3=anonymous. user_type_description contains the text description for those numbers.

    – Martincho
    Nov 13 '18 at 0:22






  • 1





    I can't follow you if you don't show sample data..

    – dwir182
    Nov 13 '18 at 0:29











  • Ok! Let me see. I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2) Hope that helps a bit.

    – Martincho
    Nov 13 '18 at 0:35






  • 1





    Put it on your question to provide more info..

    – dwir182
    Nov 13 '18 at 0:37















1















I have a kind of tricky question for this query. First the code:



SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
FROM incident
INNER JOIN user ON incident.user_id=user.user_id
INNER JOIN user_type ON user.user_type=user_type.user_type
WHERE incident.code=2
GROUP BY user.user_type


What Am I doing?



For example, I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2).



this brings the following result:



+-----------------------+----------+
| user_type_description | Quantity |
+-----------------------+----------+
| Admin | 6 |
| Moderator | 8 |
| Fully_registered_user | 8 |
| anonymous_user | 9 |
+-----------------------+----------+


Basically Admin,Moderator and Fully_registered_user are appropriately registered users. I need to add them in a result where it shows like:



+--------------+------------+
| Proper_users | Anonymous |
+--------------+------------+
| 22 | 9 |
+--------------+------------+


I am not good with sql. Any help is appreciated. Thanks.










share|improve this question



















  • 1





    Show your sample data and expected result with formatted table.. You can use This And your group by is weird.. Your select use user_type.user_type_description but your group by user.user_type

    – dwir182
    Nov 13 '18 at 0:09












  • I used your link, but stackoverflow just omits spaces. Yes, I group by user type, which is a number. 0=admin 1=mod 2=fully_reg_user 3=anonymous. user_type_description contains the text description for those numbers.

    – Martincho
    Nov 13 '18 at 0:22






  • 1





    I can't follow you if you don't show sample data..

    – dwir182
    Nov 13 '18 at 0:29











  • Ok! Let me see. I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2) Hope that helps a bit.

    – Martincho
    Nov 13 '18 at 0:35






  • 1





    Put it on your question to provide more info..

    – dwir182
    Nov 13 '18 at 0:37













1












1








1








I have a kind of tricky question for this query. First the code:



SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
FROM incident
INNER JOIN user ON incident.user_id=user.user_id
INNER JOIN user_type ON user.user_type=user_type.user_type
WHERE incident.code=2
GROUP BY user.user_type


What Am I doing?



For example, I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2).



this brings the following result:



+-----------------------+----------+
| user_type_description | Quantity |
+-----------------------+----------+
| Admin | 6 |
| Moderator | 8 |
| Fully_registered_user | 8 |
| anonymous_user | 9 |
+-----------------------+----------+


Basically Admin,Moderator and Fully_registered_user are appropriately registered users. I need to add them in a result where it shows like:



+--------------+------------+
| Proper_users | Anonymous |
+--------------+------------+
| 22 | 9 |
+--------------+------------+


I am not good with sql. Any help is appreciated. Thanks.










share|improve this question
















I have a kind of tricky question for this query. First the code:



SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
FROM incident
INNER JOIN user ON incident.user_id=user.user_id
INNER JOIN user_type ON user.user_type=user_type.user_type
WHERE incident.code=2
GROUP BY user.user_type


What Am I doing?



For example, I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2).



this brings the following result:



+-----------------------+----------+
| user_type_description | Quantity |
+-----------------------+----------+
| Admin | 6 |
| Moderator | 8 |
| Fully_registered_user | 8 |
| anonymous_user | 9 |
+-----------------------+----------+


Basically Admin,Moderator and Fully_registered_user are appropriately registered users. I need to add them in a result where it shows like:



+--------------+------------+
| Proper_users | Anonymous |
+--------------+------------+
| 22 | 9 |
+--------------+------------+


I am not good with sql. Any help is appreciated. Thanks.







mysql sql rows






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 0:47







Martincho

















asked Nov 13 '18 at 0:02









MartinchoMartincho

188




188







  • 1





    Show your sample data and expected result with formatted table.. You can use This And your group by is weird.. Your select use user_type.user_type_description but your group by user.user_type

    – dwir182
    Nov 13 '18 at 0:09












  • I used your link, but stackoverflow just omits spaces. Yes, I group by user type, which is a number. 0=admin 1=mod 2=fully_reg_user 3=anonymous. user_type_description contains the text description for those numbers.

    – Martincho
    Nov 13 '18 at 0:22






  • 1





    I can't follow you if you don't show sample data..

    – dwir182
    Nov 13 '18 at 0:29











  • Ok! Let me see. I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2) Hope that helps a bit.

    – Martincho
    Nov 13 '18 at 0:35






  • 1





    Put it on your question to provide more info..

    – dwir182
    Nov 13 '18 at 0:37












  • 1





    Show your sample data and expected result with formatted table.. You can use This And your group by is weird.. Your select use user_type.user_type_description but your group by user.user_type

    – dwir182
    Nov 13 '18 at 0:09












  • I used your link, but stackoverflow just omits spaces. Yes, I group by user type, which is a number. 0=admin 1=mod 2=fully_reg_user 3=anonymous. user_type_description contains the text description for those numbers.

    – Martincho
    Nov 13 '18 at 0:22






  • 1





    I can't follow you if you don't show sample data..

    – dwir182
    Nov 13 '18 at 0:29











  • Ok! Let me see. I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2) Hope that helps a bit.

    – Martincho
    Nov 13 '18 at 0:35






  • 1





    Put it on your question to provide more info..

    – dwir182
    Nov 13 '18 at 0:37







1




1





Show your sample data and expected result with formatted table.. You can use This And your group by is weird.. Your select use user_type.user_type_description but your group by user.user_type

– dwir182
Nov 13 '18 at 0:09






Show your sample data and expected result with formatted table.. You can use This And your group by is weird.. Your select use user_type.user_type_description but your group by user.user_type

– dwir182
Nov 13 '18 at 0:09














I used your link, but stackoverflow just omits spaces. Yes, I group by user type, which is a number. 0=admin 1=mod 2=fully_reg_user 3=anonymous. user_type_description contains the text description for those numbers.

– Martincho
Nov 13 '18 at 0:22





I used your link, but stackoverflow just omits spaces. Yes, I group by user type, which is a number. 0=admin 1=mod 2=fully_reg_user 3=anonymous. user_type_description contains the text description for those numbers.

– Martincho
Nov 13 '18 at 0:22




1




1





I can't follow you if you don't show sample data..

– dwir182
Nov 13 '18 at 0:29





I can't follow you if you don't show sample data..

– dwir182
Nov 13 '18 at 0:29













Ok! Let me see. I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2) Hope that helps a bit.

– Martincho
Nov 13 '18 at 0:35





Ok! Let me see. I am counting police reports of robbery, made from different kind of users. In my example, "admin" users reported 6 incidents of code "2" (robbery) and so on, as is showed in 'where' clause (incident must be robbery, also code 2) Hope that helps a bit.

– Martincho
Nov 13 '18 at 0:35




1




1





Put it on your question to provide more info..

– dwir182
Nov 13 '18 at 0:37





Put it on your question to provide more info..

– dwir182
Nov 13 '18 at 0:37












3 Answers
3






active

oldest

votes


















2














You can try to use condition aggregate function base on your current result set.



SUM with CASE WHEN expression.



SELECT SUM(CASE WHEN user_type_description IN ('Admin','Moderator','Fully_registered_user') THEN Quantity END) Proper_users,
SUM(CASE WHEN user_type_description = 'anonymous_user' THEN Quantity END) Anonymous
FROM (
SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
FROM incident
INNER JOIN user ON incident.user_id=user.user_id
INNER JOIN user_type ON user.user_type=user_type.user_type
WHERE incident.code=2
GROUP BY user.user_type
) t1





share|improve this answer























  • This worked perfectly. Thank you so much.

    – Martincho
    Nov 13 '18 at 1:03


















2














You just need conditional aggregation:



SELECT SUM( ut.user_type_description IN ('Admin', 'Moderator', 'Fully_registered_user') ) as Proper_users,
SUM( ut.user_type_description IN ('anonymous_user') as anonymous
FROM incident i INNER JOIN
user u
ON i.user_id = u.user_id INNER JOIN
user_type ut
ON u.user_type = ut.user_type
WHERE i.code = 2;


Notes:



  • Table aliases make the query easier to write and to read.

  • This uses a MySQL shortcut for adding values -- just just adding the booelean expressions.





share|improve this answer






























    1














    I would solve it with a CTE, but it would be better to have this association in a table.



    WITH
    user_type_categories
    AS
    (
    SELECT 'Admin' AS [user_type_description] , 'Proper_users' AS [user_type_category]
    UNION SELECT 'Moderator' AS [user_type_description] , 'Proper_users' AS [user_type_category]
    UNION SELECT 'Fully_registered_user' AS [user_type_description] , 'Proper_users' AS [user_type_category]
    UNION SELECT 'anonymous_user' AS [user_type_description] , 'Anonymous' AS [user_type_category]
    )
    SELECT
    CASE WHEN utc.[user_type_category] = 'Proper_users' THEN
    SUM(incident.user_id)
    END AS [Proper_Users_Quantity]
    , CASE WHEN utc.[user_type_category] = 'Anonymous' THEN
    SUM(incident.user_id)
    END AS [Anonymous_Quantity]
    FROM
    [incident]
    INNER JOIN [user] ON [incident].[user_id] = [user].[user_id]
    INNER JOIN [user_type] ON [user].[user_type] = [user_type].[user_type]
    LEFT JOIN user_type_categories AS utc ON utc.[user_type_description] = [user_type].[user_type_description]
    WHERE
    [incident].[code] = 2





    share|improve this answer

























    • Holy guacamole. I am not allowed to put this code but I will take note of this solution.

      – Martincho
      Nov 13 '18 at 0:53










    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%2f53271874%2fquery-i-have-4-rows-need-to-add-the-results-from-3-rows-into-one-and-leave-th%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









    2














    You can try to use condition aggregate function base on your current result set.



    SUM with CASE WHEN expression.



    SELECT SUM(CASE WHEN user_type_description IN ('Admin','Moderator','Fully_registered_user') THEN Quantity END) Proper_users,
    SUM(CASE WHEN user_type_description = 'anonymous_user' THEN Quantity END) Anonymous
    FROM (
    SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
    FROM incident
    INNER JOIN user ON incident.user_id=user.user_id
    INNER JOIN user_type ON user.user_type=user_type.user_type
    WHERE incident.code=2
    GROUP BY user.user_type
    ) t1





    share|improve this answer























    • This worked perfectly. Thank you so much.

      – Martincho
      Nov 13 '18 at 1:03















    2














    You can try to use condition aggregate function base on your current result set.



    SUM with CASE WHEN expression.



    SELECT SUM(CASE WHEN user_type_description IN ('Admin','Moderator','Fully_registered_user') THEN Quantity END) Proper_users,
    SUM(CASE WHEN user_type_description = 'anonymous_user' THEN Quantity END) Anonymous
    FROM (
    SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
    FROM incident
    INNER JOIN user ON incident.user_id=user.user_id
    INNER JOIN user_type ON user.user_type=user_type.user_type
    WHERE incident.code=2
    GROUP BY user.user_type
    ) t1





    share|improve this answer























    • This worked perfectly. Thank you so much.

      – Martincho
      Nov 13 '18 at 1:03













    2












    2








    2







    You can try to use condition aggregate function base on your current result set.



    SUM with CASE WHEN expression.



    SELECT SUM(CASE WHEN user_type_description IN ('Admin','Moderator','Fully_registered_user') THEN Quantity END) Proper_users,
    SUM(CASE WHEN user_type_description = 'anonymous_user' THEN Quantity END) Anonymous
    FROM (
    SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
    FROM incident
    INNER JOIN user ON incident.user_id=user.user_id
    INNER JOIN user_type ON user.user_type=user_type.user_type
    WHERE incident.code=2
    GROUP BY user.user_type
    ) t1





    share|improve this answer













    You can try to use condition aggregate function base on your current result set.



    SUM with CASE WHEN expression.



    SELECT SUM(CASE WHEN user_type_description IN ('Admin','Moderator','Fully_registered_user') THEN Quantity END) Proper_users,
    SUM(CASE WHEN user_type_description = 'anonymous_user' THEN Quantity END) Anonymous
    FROM (
    SELECT user_type.user_type_description,COUNT(incident.user_id) as Quantity
    FROM incident
    INNER JOIN user ON incident.user_id=user.user_id
    INNER JOIN user_type ON user.user_type=user_type.user_type
    WHERE incident.code=2
    GROUP BY user.user_type
    ) t1






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 0:50









    D-ShihD-Shih

    26.5k61532




    26.5k61532












    • This worked perfectly. Thank you so much.

      – Martincho
      Nov 13 '18 at 1:03

















    • This worked perfectly. Thank you so much.

      – Martincho
      Nov 13 '18 at 1:03
















    This worked perfectly. Thank you so much.

    – Martincho
    Nov 13 '18 at 1:03





    This worked perfectly. Thank you so much.

    – Martincho
    Nov 13 '18 at 1:03













    2














    You just need conditional aggregation:



    SELECT SUM( ut.user_type_description IN ('Admin', 'Moderator', 'Fully_registered_user') ) as Proper_users,
    SUM( ut.user_type_description IN ('anonymous_user') as anonymous
    FROM incident i INNER JOIN
    user u
    ON i.user_id = u.user_id INNER JOIN
    user_type ut
    ON u.user_type = ut.user_type
    WHERE i.code = 2;


    Notes:



    • Table aliases make the query easier to write and to read.

    • This uses a MySQL shortcut for adding values -- just just adding the booelean expressions.





    share|improve this answer



























      2














      You just need conditional aggregation:



      SELECT SUM( ut.user_type_description IN ('Admin', 'Moderator', 'Fully_registered_user') ) as Proper_users,
      SUM( ut.user_type_description IN ('anonymous_user') as anonymous
      FROM incident i INNER JOIN
      user u
      ON i.user_id = u.user_id INNER JOIN
      user_type ut
      ON u.user_type = ut.user_type
      WHERE i.code = 2;


      Notes:



      • Table aliases make the query easier to write and to read.

      • This uses a MySQL shortcut for adding values -- just just adding the booelean expressions.





      share|improve this answer

























        2












        2








        2







        You just need conditional aggregation:



        SELECT SUM( ut.user_type_description IN ('Admin', 'Moderator', 'Fully_registered_user') ) as Proper_users,
        SUM( ut.user_type_description IN ('anonymous_user') as anonymous
        FROM incident i INNER JOIN
        user u
        ON i.user_id = u.user_id INNER JOIN
        user_type ut
        ON u.user_type = ut.user_type
        WHERE i.code = 2;


        Notes:



        • Table aliases make the query easier to write and to read.

        • This uses a MySQL shortcut for adding values -- just just adding the booelean expressions.





        share|improve this answer













        You just need conditional aggregation:



        SELECT SUM( ut.user_type_description IN ('Admin', 'Moderator', 'Fully_registered_user') ) as Proper_users,
        SUM( ut.user_type_description IN ('anonymous_user') as anonymous
        FROM incident i INNER JOIN
        user u
        ON i.user_id = u.user_id INNER JOIN
        user_type ut
        ON u.user_type = ut.user_type
        WHERE i.code = 2;


        Notes:



        • Table aliases make the query easier to write and to read.

        • This uses a MySQL shortcut for adding values -- just just adding the booelean expressions.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 1:18









        Gordon LinoffGordon Linoff

        786k35310416




        786k35310416





















            1














            I would solve it with a CTE, but it would be better to have this association in a table.



            WITH
            user_type_categories
            AS
            (
            SELECT 'Admin' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Moderator' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Fully_registered_user' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'anonymous_user' AS [user_type_description] , 'Anonymous' AS [user_type_category]
            )
            SELECT
            CASE WHEN utc.[user_type_category] = 'Proper_users' THEN
            SUM(incident.user_id)
            END AS [Proper_Users_Quantity]
            , CASE WHEN utc.[user_type_category] = 'Anonymous' THEN
            SUM(incident.user_id)
            END AS [Anonymous_Quantity]
            FROM
            [incident]
            INNER JOIN [user] ON [incident].[user_id] = [user].[user_id]
            INNER JOIN [user_type] ON [user].[user_type] = [user_type].[user_type]
            LEFT JOIN user_type_categories AS utc ON utc.[user_type_description] = [user_type].[user_type_description]
            WHERE
            [incident].[code] = 2





            share|improve this answer

























            • Holy guacamole. I am not allowed to put this code but I will take note of this solution.

              – Martincho
              Nov 13 '18 at 0:53















            1














            I would solve it with a CTE, but it would be better to have this association in a table.



            WITH
            user_type_categories
            AS
            (
            SELECT 'Admin' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Moderator' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Fully_registered_user' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'anonymous_user' AS [user_type_description] , 'Anonymous' AS [user_type_category]
            )
            SELECT
            CASE WHEN utc.[user_type_category] = 'Proper_users' THEN
            SUM(incident.user_id)
            END AS [Proper_Users_Quantity]
            , CASE WHEN utc.[user_type_category] = 'Anonymous' THEN
            SUM(incident.user_id)
            END AS [Anonymous_Quantity]
            FROM
            [incident]
            INNER JOIN [user] ON [incident].[user_id] = [user].[user_id]
            INNER JOIN [user_type] ON [user].[user_type] = [user_type].[user_type]
            LEFT JOIN user_type_categories AS utc ON utc.[user_type_description] = [user_type].[user_type_description]
            WHERE
            [incident].[code] = 2





            share|improve this answer

























            • Holy guacamole. I am not allowed to put this code but I will take note of this solution.

              – Martincho
              Nov 13 '18 at 0:53













            1












            1








            1







            I would solve it with a CTE, but it would be better to have this association in a table.



            WITH
            user_type_categories
            AS
            (
            SELECT 'Admin' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Moderator' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Fully_registered_user' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'anonymous_user' AS [user_type_description] , 'Anonymous' AS [user_type_category]
            )
            SELECT
            CASE WHEN utc.[user_type_category] = 'Proper_users' THEN
            SUM(incident.user_id)
            END AS [Proper_Users_Quantity]
            , CASE WHEN utc.[user_type_category] = 'Anonymous' THEN
            SUM(incident.user_id)
            END AS [Anonymous_Quantity]
            FROM
            [incident]
            INNER JOIN [user] ON [incident].[user_id] = [user].[user_id]
            INNER JOIN [user_type] ON [user].[user_type] = [user_type].[user_type]
            LEFT JOIN user_type_categories AS utc ON utc.[user_type_description] = [user_type].[user_type_description]
            WHERE
            [incident].[code] = 2





            share|improve this answer















            I would solve it with a CTE, but it would be better to have this association in a table.



            WITH
            user_type_categories
            AS
            (
            SELECT 'Admin' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Moderator' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'Fully_registered_user' AS [user_type_description] , 'Proper_users' AS [user_type_category]
            UNION SELECT 'anonymous_user' AS [user_type_description] , 'Anonymous' AS [user_type_category]
            )
            SELECT
            CASE WHEN utc.[user_type_category] = 'Proper_users' THEN
            SUM(incident.user_id)
            END AS [Proper_Users_Quantity]
            , CASE WHEN utc.[user_type_category] = 'Anonymous' THEN
            SUM(incident.user_id)
            END AS [Anonymous_Quantity]
            FROM
            [incident]
            INNER JOIN [user] ON [incident].[user_id] = [user].[user_id]
            INNER JOIN [user_type] ON [user].[user_type] = [user_type].[user_type]
            LEFT JOIN user_type_categories AS utc ON utc.[user_type_description] = [user_type].[user_type_description]
            WHERE
            [incident].[code] = 2






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 1:24

























            answered Nov 13 '18 at 0:45









            aduguidaduguid

            2,23661132




            2,23661132












            • Holy guacamole. I am not allowed to put this code but I will take note of this solution.

              – Martincho
              Nov 13 '18 at 0:53

















            • Holy guacamole. I am not allowed to put this code but I will take note of this solution.

              – Martincho
              Nov 13 '18 at 0:53
















            Holy guacamole. I am not allowed to put this code but I will take note of this solution.

            – Martincho
            Nov 13 '18 at 0:53





            Holy guacamole. I am not allowed to put this code but I will take note of this solution.

            – Martincho
            Nov 13 '18 at 0:53

















            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%2f53271874%2fquery-i-have-4-rows-need-to-add-the-results-from-3-rows-into-one-and-leave-th%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

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)