mysql—计算列的非重复值,而不考虑其他列中的更改

wbgh16ku  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(266)

我有下表:

+---------+---------+--------+------+
| Whisper | Client  | Tenant | Ring |
+---------+---------+--------+------+
| W1      | iOS     | T3     | R1   |
+---------+---------+--------+------+
| W2      | iOS     | T2     | R1   |
+---------+---------+--------+------+
| W1      | Android | T3     | R1   |
+---------+---------+--------+------+
| W2      | Android | T2     | R1   |
+---------+---------+--------+------+
| W3      | Android | T4     | R2   |
+---------+---------+--------+------+

我想计算清楚客户栏有变化时的不同耳语,目前我使用的是:

WhipserCounts =
SELECT COUNT(DISTINCT Whipser) AS Whispers,
                Client,
                Tenant,
                Ring
FROM InputData
GROUP BY Client, Tenant, Ring;

但是我看到w1和w2对每个客户机都计算了两次,但是无论客户机是什么,我都需要将它们视为一个,所以第一次如果我得到ios,我需要将客户机设置为第一个值(知道我同时需要输出表中的客户机)。
我得到的是:

+----------+---------+--------+------+
| Whispers | Client  | Tenant | Ring |
+----------+---------+--------+------+
| 1        | iOS     | T3     | R1   |
+----------+---------+--------+------+
| 1        | iOS     | T2     | R1   |
+----------+---------+--------+------+
| 1        | Android | T3     | R1   |
+----------+---------+--------+------+
| 1        | Android | T2     | R1   |
+----------+---------+--------+------+
| 1        | Android | T4     | R2   |
+----------+---------+--------+------+

我需要的是:

+----------+---------+--------+------+
| Whispers | Client  | Tenant | Ring |
+----------+---------+--------+------+
| 1        | iOS     | T3     | R1   |
+----------+---------+--------+------+
| 1        | iOS     | T2     | R1   |
+----------+---------+--------+------+
| 1        | Android | T4     | R2   |
+----------+---------+--------+------+

你能帮个忙吗?

c90pui9n

c90pui9n1#

要做到这一点,你需要使用 MAX() ,不是分组依据:

SELECT COUNT(DISTINCT Whipser) AS Whispers,
                MAX(Client),
                Tenant,
                Ring
FROM InputData
GROUP BY Tenant, Ring;

相关问题