SQL count occurrence of every record (integer) in one table with multiple columns

DataWarrior Niño

I am working on an algorithm, using SQL and JAVA, concerning big datasets. In SQL I have a table with all the data and I want to use as much of SQL queries as possible before loading it into JAVA.

I generate random datasets (in Java), consisting exclusively of integers between 1 and 40001 and then insert them into a MySQL table. The rows can be of different lengths, with a maximum of 30 items/records (this includes the ID). So normally the amount of columns is 30 (so COL1, COL2, COL3,......COL30) but this amount will also be random at some point

What I want to do is count the occurrence of every distinct item in a table/dataset and put them in a new table with their count. This however is tricky since I want to count it over the entire table, not just one column. How do I do this?

To specify:

Take this table for example (this is a very small one in comparison with my usual tables):

ID | COL1 | COL2 | COL3 | COL4 | COL5 |
---------------------------------------
1  |   8  |  35  |  42  |  12  |  27  |
2  |  22  |  42  |  35  |  8   | NULL |
3  |  18  |  22  |   8  | NULL | NULL |
4  |  42  |  12  |  27  |  35  |  8   |
5  |  18  |  27  |  12  |  22  | NULL |

What I want to extract from this table is this:

Item | Count
-------------
8    |  3
35   |  3
40   |  1
12   |  3
27   |  3
22   |  3
42   |  2 
43   |  1
18   |  2

It is also the case that an item can't be in the same row more than once, if that helps.

Can anyone help me? Or can it just simply not be done in SQL? Would it be better to do this in JAVA, performance-wise?

Thanks in advance!

Gordon Linoff

You can do this by unpivoting the data and then aggregating:

select col, count(*)
from (select col1 as col from t union all
      select col2 from t union all
      . . . 
      select col30 from t
     ) t
group by col;

If you don't have a known set of columns, then you will need to use dynamic SQL.

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

generate a unique integer ID from multiple columns of a SQL table

分類Dev

count of distinct columns in a record

分類Dev

Delete a record based on multiple table choices SQL

分類Dev

SQL Select COUNT for Multiple Columns in a single Query

分類Dev

Grouping SQL columns from one table

分類Dev

Count of data from one column into multiple columns with MySQL?

分類Dev

MySQL count across multiple columns, with subquery, with one result query

分類Dev

Display a single SQL columns into multiple columns of an HTML table using PHP

分類Dev

Count the occurrence of one column based on another

分類Dev

Multiple SELECT Statement using One Table of SQL

分類Dev

SQL interview- how to get the record if there is no record or count is zero using multiple tables

分類Dev

R data table unique record count based on all combination of a given list of values from 2 columns

分類Dev

SQL: create a recordset by repeating the output of one query for every record of a second query

分類Dev

How to get COUNT(*) from one partition of a table in SQL Server 2012?

分類Dev

SQL Server: Combine multiple rows into one with no overlap but new columns

分類Dev

SQL/VBA - Multiple Columns from one column based on criteria

分類Dev

Optimizing Query based on Inserting into multiple columns into target table from one column of a source table

分類Dev

not able to display record count in data table

分類Dev

How to count records from multiple columns eliminating null values in hive table

分類Dev

Count the occurrence of a value among multiple associative arrays in JavaScript

分類Dev

SQL Create Table Columns

分類Dev

Group SQL Columns and count the results

分類Dev

Delete Rows from one table when specific columns are updated on other linked table - SQL Trigger

分類Dev

Select data from one table & then rename the columns based on another table in SQL server

分類Dev

Easiest possible way to retrieve MYSQL data in a table field with decreasing order of occurrence along with the occurrence count?

分類Dev

Sum of multiple columns in SQL

分類Dev

Get a table with one row per id and multiple columns from a table with multiple rows for same id and calculate sum based on condition

分類Dev

sql - how to select multiple columns with only one distinct column from joining multiple tables

分類Dev

SQL Server parse XML to table - multiple node with the same name and first node should be table columns

Related 関連記事

  1. 1

    generate a unique integer ID from multiple columns of a SQL table

  2. 2

    count of distinct columns in a record

  3. 3

    Delete a record based on multiple table choices SQL

  4. 4

    SQL Select COUNT for Multiple Columns in a single Query

  5. 5

    Grouping SQL columns from one table

  6. 6

    Count of data from one column into multiple columns with MySQL?

  7. 7

    MySQL count across multiple columns, with subquery, with one result query

  8. 8

    Display a single SQL columns into multiple columns of an HTML table using PHP

  9. 9

    Count the occurrence of one column based on another

  10. 10

    Multiple SELECT Statement using One Table of SQL

  11. 11

    SQL interview- how to get the record if there is no record or count is zero using multiple tables

  12. 12

    R data table unique record count based on all combination of a given list of values from 2 columns

  13. 13

    SQL: create a recordset by repeating the output of one query for every record of a second query

  14. 14

    How to get COUNT(*) from one partition of a table in SQL Server 2012?

  15. 15

    SQL Server: Combine multiple rows into one with no overlap but new columns

  16. 16

    SQL/VBA - Multiple Columns from one column based on criteria

  17. 17

    Optimizing Query based on Inserting into multiple columns into target table from one column of a source table

  18. 18

    not able to display record count in data table

  19. 19

    How to count records from multiple columns eliminating null values in hive table

  20. 20

    Count the occurrence of a value among multiple associative arrays in JavaScript

  21. 21

    SQL Create Table Columns

  22. 22

    Group SQL Columns and count the results

  23. 23

    Delete Rows from one table when specific columns are updated on other linked table - SQL Trigger

  24. 24

    Select data from one table & then rename the columns based on another table in SQL server

  25. 25

    Easiest possible way to retrieve MYSQL data in a table field with decreasing order of occurrence along with the occurrence count?

  26. 26

    Sum of multiple columns in SQL

  27. 27

    Get a table with one row per id and multiple columns from a table with multiple rows for same id and calculate sum based on condition

  28. 28

    sql - how to select multiple columns with only one distinct column from joining multiple tables

  29. 29

    SQL Server parse XML to table - multiple node with the same name and first node should be table columns

ホットタグ

アーカイブ