admin

如何计算两列中具有相同值的行(SQL)?

sql

我确信必须有一个相对简单的方法来执行此操作,但是此刻正在使我逃脱。假设我有一个这样的SQL表:

+-----+-----+-----+-----+-----+
|  A  |  B  |  C  |  D  |  E  |
+=====+=====+=====+=====+=====+
|  1  |  2  |  3  | foo | bar | << 1,2
+-----+-----+-----+-----+-----+
|  1  |  3  |  3  | biz | bar | << 1,3
+-----+-----+-----+-----+-----+
|  1  |  2  |  4  |  x  |  y  | << 1,2
+-----+-----+-----+-----+-----+
|  1  |  2  |  5  | foo | bar | << 1,2
+-----+-----+-----+-----+-----+
|  4  |  2  |  3  | foo | bar | << 4,2
+-----+-----+-----+-----+-----+
|  1  |  3  |  3  | foo | bar | << 1,3
+-----+-----+-----+-----+-----+

现在,我想知道A和B列的每种值组合出现多少次,而与其他列无关。因此,在此示例中,我想要这样的输出:

+-----+-----+-----+
|  A  |  B  |count|
+=====+=====+=====+
|  1  |  2  |  3  |
+-----+-----+-----+
|  1  |  3  |  2  |
+-----+-----+-----+
|  4  |  2  |  1  |
+-----+-----+-----+

用什么SQL来确定呢?我觉得这绝对不是一件很不常见的事情。

谢谢!


阅读 190

收藏
2021-05-10

共1个答案

admin

SELECT A,B,COUNT(*)
FROM the-table
GROUP BY A,B
2021-05-10