简体   繁体   English

计算列的不同值,而不管其他列的变化

[英]Count distinct value of column regardless change in other columns

I have the following table:我有下表:

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

And I want to count distinct Whispers knowing that there is a change in client column, currently I'm using this:我想计算不同的耳语,知道客户列发生了变化,目前我正在使用这个:

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

But I see that W1 and W2 are counted twice for each client but I need to consider them as one no matter the client is, so for the first time If I got iOS I need to set the client by the first value.但是我看到每个客户端的 W1 和 W2 被计算两次,但是无论客户端是什么,我都需要将它们视为一个,所以第一次如果我得到 iOS,我需要将客户端设置为第一个值。 (Knowing that I need the client in the output table in the same time). (知道同时需要output表中的client)。

What I got:我得到了什么:

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

What I need:我需要的:

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

Could you please help with this?你能帮忙吗?

To do that, you need to use MAX() , not group by:为此,您需要使用MAX() ,而不是分组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM