识别不同的行

时间:2013-11-22 20:35:59

标签: sql oracle group-by distinct aggregate

考虑下表:table1

+----+-------+------+--------+
| Id | AppId | Req  | Status |
+----+-------+------+--------+
|  1 | app1  | r1   | m      |
|  2 | app1  | r1   | n      |
|  3 | app1  | r2   | m      |
|  4 | app1  | r3   | m      |
|  5 | app2  | r1   | m      |
|  6 | app2  | r1   | m      |
|  7 | app3  | r1   | m      |
|  8 | app3  | r2   | m      |
|  9 | app3  | r3   | m      |
+----+-------+------+--------+

SELECT 
    T1.AppId,
    COUNT(*)
FROM TABLE1 T1
WHERE T1.Req = r1
GROUP BY T1.AppId
HAVING COUNT(*) > 1

这会给我以下输出:

+------+---+
| app1 | 2 |
+------+---+
| app2 | 2 |
+------+---+

我希望输出如下所示,只有在状态不同时才会添加输出。 例如,在这种情况下,app2 r1状态为“m”。它没有区别。

app1     2

请帮我怎么写这个查询。

1 个答案:

答案 0 :(得分:2)

您是否尝试过HAVING COUNT(distinct status) > 1

像这样:

SELECT
T1.AppId,
COUNT(*)
FROM TABLE1 T1
WHERE T1.Req = r1
GROUP BY T1.AppId
HAVING COUNT(distinct status) > 1