SQL - 每个组合的总和

时间:2017-10-24 19:57:21

标签: mysql sql database

我有3列的表:A,B和C.这些列可以是true或false。 我想了解每一种可能的组合。

示例数据:

CREATE TABLE `myTable` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `A` mediumint default NULL,
  `B` mediumint default NULL,
  `C` mediumint default NULL,
  PRIMARY KEY (`id`)
) AUTO_INCREMENT=1;

INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (0,0,1),(1,1,0),(0,0,0),(1,1,0),(1,0,0),(1,0,1),(0,0,1),(1,1,1),(0,1,0),(1,1,1);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (1,0,1),(0,1,0),(1,1,1),(0,0,1),(1,0,0),(0,0,0),(0,0,1),(1,1,0),(0,0,0),(1,1,0);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (1,1,0),(0,1,0),(1,1,1),(0,0,0),(1,1,0),(1,0,1),(1,1,1),(1,0,1),(1,1,1),(1,1,1);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (0,1,0),(1,0,0),(0,1,0),(0,0,0),(0,0,0),(1,0,0),(1,0,1),(1,1,1),(0,0,1),(0,0,0);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (1,1,1),(0,0,1),(1,1,0),(1,1,0),(1,0,0),(0,0,1),(0,1,1),(1,0,1),(1,0,0),(1,1,0);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (1,1,1),(0,0,0),(1,0,1),(1,0,0),(1,0,0),(1,0,0),(0,0,1),(1,1,1),(0,1,1),(1,1,0);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (0,1,1),(0,1,1),(0,1,0),(0,0,0),(0,1,0),(0,1,1),(0,1,1),(0,1,1),(0,1,0),(0,1,0);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (0,1,1),(0,0,1),(0,1,0),(1,1,0),(0,0,0),(1,1,1),(1,1,0),(0,1,1),(1,0,1),(1,0,0);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (0,1,0),(1,1,1),(0,1,0),(1,1,0),(1,0,1),(1,1,0),(0,1,0),(0,1,0),(0,1,0),(0,1,0);
INSERT INTO `myTable` (`A`,`B`,`C`) VALUES (1,1,0),(0,1,0),(1,1,1),(0,0,0),(1,0,0),(1,1,0),(1,0,1),(0,0,1),(1,0,1),(1,0,0);

示例结果(来自样本数据):

组合:计数 无:11
答:12
B:17
C:10
AB:16
BC:9
AC:11
ABC:14

这可以在一个查询中使用吗? (MySQL的)

4 个答案:

答案 0 :(得分:3)

这似乎是一个简单的countGroup by

SELECT A, B, C, count(*)
FROM MyTable
GROUP BY A, B, C;

DEMO:

如果你想要,你可以显示组合值使用concat和case ...

SELECT concat(case when A = 1 then 'A' else '' end, 
              case when B = 1 then 'B' else '' end, 
              case when C = 1 then 'C' else '' end)  as Combination
     , count(*)
FROM MyTable
GROUP BY A, B, C
ORDER BY Combination;

或Paul Spiegel在评论中表示:

SELECT concat(left('A', A), left('B', B), left('C', C)) as Combination
     , count(*)
FROM MyTable
GROUP BY A, B, C
ORDER BY Combination;

给我们:

+----+-------------+----------+
|    | Combination | count(*) |
+----+-------------+----------+
|  1 |             |       11 |
|  2 | A           |       12 |
|  3 | AB          |       16 |
|  4 | ABC         |       14 |
|  5 | AC          |       11 |
|  6 | B           |       17 |
|  7 | BC          |        9 |
|  8 | C           |       10 |
+----+-------------+----------+

答案 1 :(得分:2)

假设您的组合位于这些列的位置。值为TRUE,您只需在这3列上查看group bycase逻辑就是在单列中显示组合;您可以轻松替换" case ... end"用" A, B, C"用这些列分别显示它们的值得到相同的结果。

select 
case 
    when A = 1 and B = 1 and C = 1  then 'ABC'
    when A = 1 and B = 1 and C = 0 then 'AB'
    when A = 1 and B = 0 and C = 1 then 'AC'
    when A = 0 and B = 1 and C = 1 then 'BC'
    when A = 1 and B = 0 and C = 0 then 'A'
    when A = 0 and B = 1 and C = 0 then 'B'
    when A = 0 and B = 0 and C = 1 then 'C'
    else 'oops, this should not happen'
end as `Combo`
--, sum(sumThing) as `sum` --amended to count per question edit
, count(*) as `count`
from myTable
where A = true
or B = true
or C = true
group by A, B, C

答案 2 :(得分:1)

您可以为每个组合使用一个简单的子查询并将它们联合起来:

SELECT "none", COUNT(*) FROM mytable WHERE A = 0 AND B = 0 AND C = 0
UNION
SELECT "A", COUNT(*) FROM mytable WHERE A = 1 AND B = 0 AND C = 0
UNION
SELECT "B", COUNT(*) FROM mytable WHERE A = 0 AND B = 1 AND C = 0
UNION
SELECT "C", COUNT(*) FROM mytable WHERE A = 0 AND B = 0 AND C = 1
UNION
SELECT "AB", COUNT(*) FROM mytable WHERE A = 1 AND B = 1 AND C = 0
UNION
SELECT "BC", COUNT(*) FROM mytable WHERE A = 0 AND B = 1 AND C = 1
UNION
SELECT "AC", COUNT(*) FROM mytable WHERE A = 1 AND B = 0 AND C = 1
UNION
SELECT "ABC", COUNT(*) FROM mytable WHERE A = 1 AND B = 1 AND C = 1

答案 3 :(得分:1)

使用条件计数

SELECT
  COUNT(CASE WHEN A=1 THEN 1 END) AS A,
  COUNT(CASE WHEN B=1 THEN 1 END) AS B,
  COUNT(CASE WHEN C=1 THEN 1 END) AS C,
  COUNT(CASE WHEN A=1 AND B=1 THEN 1 END) AS AB,
  COUNT(CASE WHEN A=1 AND C=1 THEN 1 END) AS AC,
  COUNT(CASE WHEN B=1 AND C=1 THEN 1 END) AS BC,
  COUNT(CASE WHEN A=1 AND B=1 AND C=1 THEN 1 END) AS ABC,
  COUNT(CASE WHEN A<>1 AND B<>1 AND C<>1 THEN 1 END) AS None
FROM table1;