如何查找表的每列的唯一值计数

时间:2015-09-19 09:01:21

标签: mysql count

我想获得下表中每列的唯一值计数。对于每个唯一值,输出应该有1行,并且与起始表的列数相同,每个列都具有该唯一值的计数:

表名: - mytable

+---------+---------+---------+---------+---------+
| column1 | column2 | column3 | column4 | column5 |
+---------+---------+---------+---------+---------+
| car     | train   | plane   | tempo   | laptop  |
| bike    | pen     | car     | Null    | pencil  |
| tempo   | book    | car     | laptop  | book    |
| Null    | bike    | tempo   | book    | pen     |
+---------+---------+---------+---------+---------+

预期产出:

+---------+---------+---------+---------+---------+---------+
|         | column1 | column2 | column3 | column4 | column5 |
+---------+---------+---------+---------+---------+---------+
| car     |    1    |    0    |    2    |    0    |    0    |
| bike    |    1    |    1    |    0    |    0    |    0    |
| tempo   |    1    |    0    |    1    |    1    |    0    |
| train   |    0    |    1    |    0    |    0    |    0    |
| pen     |    0    |    1    |    0    |    0    |    1    |
| book    |    0    |    1    |    0    |    1    |    1    |
| plane   |    0    |    0    |    1    |    0    |    0    |
| laptop  |    0    |    0    |    0    |    1    |    1    |
| pencil  |    0    |    0    |    0    |    0    |    1    |
| (null)  |    1    |    0    |    0    |    1    |    0    |
+---------+---------+---------+---------+---------+---------+

5 个答案:

答案 0 :(得分:1)

我希望,这是你最终要寻找的。 一切顺利:)

SQL Fiddle

MySQL 5.5架构设置

CREATE TABLE table_name
(
  column_1 VARCHAR (20),
  column_2 VARCHAR (20),
  column_3 VARCHAR (20),
  column_4 VARCHAR (20),
  column_5 VARCHAR (20));

 insert into table_name values
('car','train','plane','tempo','laptop'); 
 insert into table_name values
('bike','pen','car',Null,'pencil'); 
 insert into table_name values
('tempo','book','car','laptop','book'); 
 insert into table_name values
(Null,'bike','tempo','book','pen'); 

查询1

SELECT
     valuex,
     SUM(IFNULL(ELT(FIELD(Columnx, 'Column_1'), 1), 0)) as Column_1,
     SUM(IFNULL(ELT(FIELD(Columnx, 'Column_2'), 1), 0)) as Column_2,
     SUM(IFNULL(ELT(FIELD(Columnx, 'Column_3'), 1), 0)) as Column_3,
     SUM(IFNULL(ELT(FIELD(Columnx, 'Column_4'), 1), 0)) as Column_4,
     SUM(IFNULL(ELT(FIELD(Columnx, 'Column_5'), 1), 0)) as Column_5
FROM (
  SELECT column_1 AS valuex, "Column_1" AS Columnx
  FROM table_name
  UNION ALL
  SELECT column_2 AS valuex, "Column_2" AS Columnx
  FROM table_name
  UNION ALL
  SELECT column_3 AS valuex, "Column_3" AS Columnx
  FROM table_name
  UNION ALL
  SELECT column_4 AS valuex, "Column_4" AS Columnx
  FROM table_name
  UNION ALL
  SELECT column_5 AS valuex, "Column_5" AS Columnx
  FROM table_name
) s
group by valuex

<强> Results

|--------|----------|----------|----------|----------|----------|
| valuex | Column_1 | Column_2 | Column_3 | Column_4 | Column_5 |
|--------|----------|----------|----------|----------|----------|
| (null) |        1 |        0 |        0 |        1 |        0 |
|   bike |        1 |        1 |        0 |        0 |        0 |
|   book |        0 |        1 |        0 |        1 |        1 |
|    car |        1 |        0 |        2 |        0 |        0 |
| laptop |        0 |        0 |        0 |        1 |        1 |
|    pen |        0 |        1 |        0 |        0 |        1 |
| pencil |        0 |        0 |        0 |        0 |        1 |
|  plane |        0 |        0 |        1 |        0 |        0 |
|  tempo |        1 |        0 |        1 |        1 |        0 |
|  train |        0 |        1 |        0 |        0 |        0 |

答案 1 :(得分:0)

你的桌子看起来没有标准化,所以我认为你应该使用更好的结构,但没有任何背景我不能建议你如何改进它。要解决您的问题,您可以使用一些UNION ALL查询,然后使用GROUP BY

SELECT value, column, COUNT(*)
FROM (
  SELECT column1 AS value, "Column1" AS Column
  FROM mytable
  UNION ALL
  SELECT column2 AS value, "Column2" AS Column
  FROM mytable
  UNION ALL
  SELECT column3 AS value, "Column3" AS Column
  FROM mytable
  UNION ALL
  SELECT column4 AS value, "Column4" AS Column
  FROM mytable
) s
GROUP BY value, column
ORDER BY value, column

如果你想在列中包含所有值,那么你可以使用它:

SELECT
  value,
  MAX(CASE WHEN column='Column1' THEN cnt END AS cnt_1,
  MAX(CASE WHEN column='Column2' THEN cnt END AS cnt_2,
  MAX(CASE WHEN column='Column3' THEN cnt END AS cnt_3,
  MAX(CASE WHEN column='Column4' THEN cnt END AS cnt_4
FROM (
  SELECT value, column, COUNT(*) AS cnt
  FROM (
    SELECT column1 AS value, "Column1" AS Column
    FROM mytable
    UNION ALL
    SELECT column2 AS value, "Column2" AS Column
    FROM mytable
    UNION ALL
    SELECT column3 AS value, "Column3" AS Column
    FROM mytable
    UNION ALL
    SELECT column4 AS value, "Column4" AS Column
    FROM mytable
  ) AS s
  GROUP BY value, column
  ORDER BY value, column
) AS s
GROUP BY value

答案 2 :(得分:0)

鉴于上表并假设,您希望找到<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="myApp" ng-controller="myCtrl"> <input type="password" id="centraPassword" placeholder="Enter Centra Password" class="form-control" ng-model="centraPassword" /> <label> <input type="checkbox" ng-model="centrapasswordcheck" > I have a Centra Travels password </label> <button type="submit" class="btn btn-info" ng-disabled="!centrapasswordcheck" id="btnContinue" ng-click="ContinueDetails()">Continue</button> </body>的不同值,这是一个起点:

column1

sqlfiddle Docker Registry

所有列与联合:

SELECT column1, count(column1) FROM myTable GROUP BY column1

here

答案 3 :(得分:0)

这是你在找什么?

SQL Fiddle

MySQL 5.5架构设置

CREATE TABLE table_name
(
  column_1 VARCHAR (20),
  column_2 VARCHAR (20),
  column_3 VARCHAR (20),
  column_4 VARCHAR (20),
  column_5 VARCHAR (20));

 insert into table_name values
('car','train','plane','tempo','laptop'); 
 insert into table_name values
('bike','pen','car',Null,'pencil'); 
 insert into table_name values
('tempo','book','car','laptop','book'); 
 insert into table_name values
(Null,'bike','tempo','book','pen'); 

查询1

select col, Count(*) from ( 
select distinct column_1 as col from table_name
union all
select distinct column_2 as col from table_name
union all
select distinct column_3 as col from table_name
union all
select distinct column_4 as col from table_name
union all
select distinct column_5 as col from table_name) as newTable
group by col

<强> Results

|    col | Count(*) |
|--------|----------|
| (null) |        2 |
|   bike |        2 |
|   book |        3 |
|    car |        2 |
| laptop |        2 |
|    pen |        2 |
| pencil |        1 |
|  plane |        1 |
|  tempo |        3 |
|  train |        1 |

答案 4 :(得分:0)

根据@ fthiella的逻辑,这里是答案,我相信这是你真正想要的:)

SQL Fiddle

MySQL 5.5架构设置

CREATE TABLE table_name
(
  column_1 VARCHAR (20),
  column_2 VARCHAR (20),
  column_3 VARCHAR (20),
  column_4 VARCHAR (20),
  column_5 VARCHAR (20));

 insert into table_name values
('car','train','plane','tempo','laptop'); 
 insert into table_name values
('bike','pen','car',Null,'pencil'); 
 insert into table_name values
('tempo','book','car','laptop','book'); 
 insert into table_name values
(Null,'bike','tempo','book','pen'); 

查询1

SELECT valueX, columnX, COUNT(*)
FROM (
  SELECT column_1 AS valuex, "Column_1" AS Columnx
  FROM table_name
  UNION ALL
  SELECT column_2 AS valuex, "Column_2" AS Columnx
  FROM table_name
  UNION ALL
  SELECT column_3 AS valuex, "Column_3" AS Columnx
  FROM table_name
  UNION ALL
  SELECT column_4 AS valuex, "Column_4" AS Columnx
  FROM table_name
) s
GROUP BY valuex, columnX
ORDER BY valuex, columnX

<强> Results

| valuex |  Columnx | COUNT(*) |
|--------|----------|----------|
| (null) | Column_1 |        1 |
| (null) | Column_4 |        1 |
|   bike | Column_1 |        1 |
|   bike | Column_2 |        1 |
|   book | Column_2 |        1 |
|   book | Column_4 |        1 |
|    car | Column_1 |        1 |
|    car | Column_3 |        2 |
| laptop | Column_4 |        1 |
|    pen | Column_2 |        1 |
|  plane | Column_3 |        1 |
|  tempo | Column_1 |        1 |
|  tempo | Column_3 |        1 |
|  tempo | Column_4 |        1 |
|  train | Column_2 |        1 |