我有这样的看法:
select Horse_Name, LevelName, TYPE_NAME, Event_Name, Result_Name
from V_Horse_Transaction
我的结果是这样的:
Horse_Name LevelName TYPE_NAME Result_Name
DXB-HORSE QUALIFIER 40 PASS
DXB-HORSE QUALIFIER 40 PASS
DXB-HORSE QUALIFIER 80 PASS
DXB-HORSE QUALIFIER 80 PASS
INDIAN-HORSE QUALIFIER 40 FAIL
DXB-HORSE QUALIFIER 40 PASS
INDIAN-HORSE QUALIFIER 40 PASS
INDIAN-HORSE QUALIFIER 80 FAIL
INDIAN-HORSE QUALIFIER 80 FAIL
在我的查询中,我只想显示马名,该马名最少为2条记录(40条记录)和2条记录(80条记录),且两种结果类型均为“ Pass”(通行证)和LevelName ='QUALIFIER'。所以我写了这样的查询。但是我没有任何记录: 我的查询是这样的:
select Horse_Name, COUNT(2)
from V_Horse_Transaction
where TYPE_NAME = 40 and TYPE_NAME = 80
and Result_Name = 'PASS' and LevelName = 'QUALIFIER'
group by TYPE_NAME, Result_Name, LevelName, Horse_Name
答案 0 :(得分:1)
您可以group by Horse_Name
子句中having
设置条件:
select Horse_Name
from V_Horse_Transaction
where TYPE_NAME in (40, 80) and Result_Name = 'PASS' and LevelName = 'QUALIFIER'
group by Horse_Name
having count(case when TYPE_NAME = 40 then 1 end) >= 2
and count(case when TYPE_NAME = 80 then 1 end) >= 2
请参见demo。
结果:
| Horse_Name |
| ---------- |
| DXB-HORSE |