PIVOT/count sum of values in a column - Mysql

Rick

I am new to MYSQL and trying to get PIVOT of my data. I have the sample table as below link:

create temporary table temp(reqtypeid int, reqcode int);
insert into temp(reqtypeid, reqcode) values (NULL,  0);
insert into temp(reqtypeid, reqcode) values (NULL,  2);
insert into temp(reqtypeid, reqcode) values ( 1  ,   0);
insert into temp(reqtypeid, reqcode) values (1 ,    1);
insert into temp(reqtypeid, reqcode) values (2   , NULL);
insert into temp(reqtypeid, reqcode) values ( 2  ,   0);
insert into temp(reqtypeid, reqcode) values (  2    ,    1);
insert into temp(reqtypeid, reqcode) values (  3    ,    1);
insert into temp(reqtypeid, reqcode) values ( 4 ,  NULL);
insert into temp(reqtypeid, reqcode) values ( 4  ,   1);

https://rextester.com/PVBI7963

My expected output is:

enter image description here

I am not intending to pivot but for every reqtypeid, I want to find the count of reqcode=null, reqcode=0/1/2. The code that I have tried is in the link. I am unable to get the correct output. Can someone help?

forpas

'NULL' is a string literal and not the same as NULL.
When comparing anything to NULL you must use the operator IS and not =.
Use conditional aggregation like this:

SELECT
    reqtypeid,  
    SUM(reqcode IS NULL) reqcode_null,
    SUM(reqcode = 0) reqcode_0,
    SUM(reqcode = 1) reqcode_1,
    SUM(reqcode = 2) reqcode_2
FROM temp
GROUP BY reqtypeid
ORDER BY reqtypeid IS NULL, reqtypeid 

See the demo.
Results:

> reqtypeid | reqcode_null | reqcode_0 | reqcode_1 | reqcode_2
> --------: | -----------: | --------: | --------: | --------:
>         1 |            0 |         1 |         1 |         0
>         2 |            1 |         1 |         1 |         0
>         3 |            0 |         0 |         1 |         0
>         4 |            1 |         0 |         1 |         0
>      null |            0 |         1 |         0 |         1

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

MySQL SUM of one column, DISTINCT of ID column

分類Dev

How to count the sum of distinct Excel values in a column?

分類Dev

MySQL SUM IF column has specific value

分類Dev

how to calculate of sum of multiple conditional values in mysql

分類Dev

How to sum the values in a column based on another column or different group?

分類Dev

Sum column values in a table based on row values of a different table

分類Dev

Sum specific MySQL table column and write the result in other column

分類Dev

How to aggregate sum, and convert unique row values to column names, in pandas?

分類Dev

Filter group of rows based on sum of values from different column

分類Dev

Excel - sum values based on distinct value in another column

分類Dev

How to sum values column wise for Array of Arrays in Scala?

分類Dev

Postgresql update record column based on it's JSONB field values sum

分類Dev

Pandas dataframe count values for each column and sum in new index

分類Dev

Excel VBA Merge Duplicate rows and sum values in certain column

分類Dev

Sum all values in every column of a data.frame in R

分類Dev

Set two values in one table based on sum of a column in another table

分類Dev

Get the sum of all values in a specific column of a ListView in QML

分類Dev

SQL Access, Sum one column's values only when ALL values in another column in are in specified range

分類Dev

mysql: difference between values in one column

分類Dev

Presto / MySQL - Regex to check column values are alphanumeric or not

分類Dev

PHP MySQL Sum a column IF row have the right timestamp

分類Dev

How do I sum column after count in mysql query

分類Dev

MySQL query with multiple random values but sum always within a range

分類Dev

MySQL - count distinct values from one column based on another column

分類Dev

mysql insert unique values from one column to a column of another table

分類Dev

how to concatenate two column values and store in a another column mysql php

分類Dev

Sum of values in a numeric column that are in the interval between two characters of another column (R)

分類Dev

Use google query as an array formula to sum values in a column based on information in another column

分類Dev

SQL: select distinct on column1 and sum up column2 values of merged rows

Related 関連記事

  1. 1

    MySQL SUM of one column, DISTINCT of ID column

  2. 2

    How to count the sum of distinct Excel values in a column?

  3. 3

    MySQL SUM IF column has specific value

  4. 4

    how to calculate of sum of multiple conditional values in mysql

  5. 5

    How to sum the values in a column based on another column or different group?

  6. 6

    Sum column values in a table based on row values of a different table

  7. 7

    Sum specific MySQL table column and write the result in other column

  8. 8

    How to aggregate sum, and convert unique row values to column names, in pandas?

  9. 9

    Filter group of rows based on sum of values from different column

  10. 10

    Excel - sum values based on distinct value in another column

  11. 11

    How to sum values column wise for Array of Arrays in Scala?

  12. 12

    Postgresql update record column based on it's JSONB field values sum

  13. 13

    Pandas dataframe count values for each column and sum in new index

  14. 14

    Excel VBA Merge Duplicate rows and sum values in certain column

  15. 15

    Sum all values in every column of a data.frame in R

  16. 16

    Set two values in one table based on sum of a column in another table

  17. 17

    Get the sum of all values in a specific column of a ListView in QML

  18. 18

    SQL Access, Sum one column's values only when ALL values in another column in are in specified range

  19. 19

    mysql: difference between values in one column

  20. 20

    Presto / MySQL - Regex to check column values are alphanumeric or not

  21. 21

    PHP MySQL Sum a column IF row have the right timestamp

  22. 22

    How do I sum column after count in mysql query

  23. 23

    MySQL query with multiple random values but sum always within a range

  24. 24

    MySQL - count distinct values from one column based on another column

  25. 25

    mysql insert unique values from one column to a column of another table

  26. 26

    how to concatenate two column values and store in a another column mysql php

  27. 27

    Sum of values in a numeric column that are in the interval between two characters of another column (R)

  28. 28

    Use google query as an array formula to sum values in a column based on information in another column

  29. 29

    SQL: select distinct on column1 and sum up column2 values of merged rows

ホットタグ

アーカイブ