0

我正在运行这个查询:

SELECT "uname","sip",count(1) as cnt FROM "random_data_date" GROUP BY "uname","sip";

要得到这样的东西:

+------------+-----------------+--------------+
|   uname    |    sip          | cnt          |
+------------+-----------------+--------------+
| root       | 172.17.165.60   | 1            |
| root       | 172.17.53.124   | 2            |
| root       | 172.28.184.39   | 3            |
| root       | 192.168.207.7   | 1            |
| root       | 192.168.245.110 | 1            |
| user1      | 172.17.165.60   | 1            |
| user1      | 172.24.85.227   | 10           |
| user1      | 172.25.14.184   | 2            |
| user2      | 172.16.194.151  | 1            |
| user2      | 172.16.248.88   | 1            |
| user2      | 172.16.9.55     | 1            |
| user2      | 172.17.165.60   | 2            |
| user2      | 172.17.234.122  | 1            |
| user2      | 172.17.53.124   | 1            |
+------------+-----------------+--------------+

有没有办法让它把sip每个用户名的 s 的数量相加,然后把cnt它们的 s 相加,所以输出是这样的:

+------------+-----------------+--------------+
|   uname    |    sipcnt       | cnt          |
+------------+-----------------+--------------+
| root       | 5               | 8            |
| user1      | 3               | 13           |
| user2      | 6               | 7            |
+------------+-----------------+--------------+

我正在使用Apache Phoenix来查询 Apache HBase。

我发现这个问题很相似,但我不知道如何将答案应用于我的情况。

4

3 回答 3

4
SELECT "uname",
       Count(distinct "sip") as "sipcnt",
       count(1) as cnt 
FROM "random_data_date" 
GROUP BY "uname";
于 2014-11-22T01:01:19.747 回答
1

会是这样的:

SELECT 
    "uname",
    count(distinct "sip"),
    count("sip") as cnt 
FROM 
    "random_data_date" 
GROUP BY 
    "uname"
;
于 2014-11-22T01:03:27.043 回答
0

运行此查询将为您提供所需的结果:

SELECT DISTINCT(uname), COUNT(sip), SUM(cnt) FROM random_data_date GROUP BY uname

于 2014-11-22T01:10:48.703 回答