+------+------+
|DealId|Failcode|
+------+------+
|1 |A |
+------+------+
|2 |A |
+------+------+
|2 |A |
+------+------+
|1 |B |
+------+------+
|2 |B |
+------+------+
|1 |A |
+------+------+
|1 |A |
+------+------+
我想选择使用不同的失败代码及其失败率多次出现的DealId
输出应类似于:
+------+------+-----+
|DealId|Fail |count|
+------+------+-----+
|1 |A |3 |
+------+------+-----+
|1 |B |1 |
+------+------+-----+
|2 |A |2 |
+------+------+-----+
|2 |B |1 |
+------+------+-----+
答案 0 :(得分:2)
使用聚合和group by
select DealId,Fail,count(*) as cnt
from tablename
group by DealId,Fail
having count(*) > 1;
答案 1 :(得分:1)
您可以像下面一样使用WHERE EXIST
来检查DealId
在不同的Failcode
中多次出现的情况。
要显示计数,您只需使用GROUP BY dealid,failcode
SELECT T1.dealid,
T1.failcode Fail,
Count(*) AS CT
FROM YourTableName T1
WHERE EXISTS (SELECT 1
FROM YourTableName T2
WHERE T2.dealid = T1.dealid
AND T2.failcode <> T1.failcode)
GROUP BY T1.dealid,
T1.failcode
注意:将YourTableName
替换为实际的表名。
答案 2 :(得分:0)
我想选择使用不同的失败代码及其失败率多次出现的DealId
我的解释是,您想要一种以上的失败率。您的样本数据未提供第一个条件过滤掉的交易ID的任何示例。但是,这个问题的答案似乎是:
select dealid, fail, cnt
from (select DealId, Fail, count(*) as cnt,
count(*) over (partition by DealId) as cnt_fails
from tablename
group by DealId, Fail
) t
having cnt_fails > 1;