sql-server - 使用带有 SQL 联合的过滤器
问题描述
我有这个 SQL Server 查询。执行此查询将给出claimnumbers 和UW_Peril 的列表。
SQL 服务器:
select
ClaimNum, UW_Peril
from
tableA
union
select
ClaimNum, UW_Peril
from
(select
claimNum, 'Litigation' as UW_Peril
from
tableB
where
Litigation = 1 and OrderofSuitReceived = 1) lt
我有2个案例:
- 如果每个索赔编号只有一行,则保持原样。
- 但是,如果每个索赔编号超过行,则选择 UW_Peril = 'Litigation' 所在的行
例子:
ClaimNum UW_Peril
--------------------------
2053057 Litigation
2053057 Theft
2053125 Litigation
2052452 Subro
预期输出
例子:
ClaimNum UW_Peril
--------------------------
2053057 Litigation
2053125 Litigation
2052452 Subro
我将如何修改它?提前致谢
解决方案
您可以使用exists
and :not exists
union all
select ClaimNum, UW_Peril
from tableB b
where exists (select 1
from tableB b1
where b1.ClaimNum = b.ClaimNum and b1.UW_Peril = b.UW_Peril)
union all
select ClaimNum, UW_Peril
from tableB b
where not exists (select 1
from tableB b1
where b1.ClaimNum = b.ClaimNum and b1.UW_Peril = b.UW_Peril) and
b.UW_Peril = 'Lititgation';
推荐阅读
- flutter - 带有圆形图像图标的颤振布局
- python-3.x - 我正在使用 bs4 进行网页抓取,但网址不会显示
- javascript - 在 Laravel 中实现 intlTelInput 的问题
- bash - 如何在函数中创建目录
- javascript - 如何使用 jquery 将数组值分配给另一个输入类型
- apache-spark - Parquet 文件大小如何随 Spark 数据集中的计数而变化
- python - 重载、引用以及介于两者之间的所有内容
- javascript - React 的 Firebase onAuthStateChanged 错误
- angular - 选中的复选框(作为选定的行)在页面移动然后返回时未选中
- jekyll - Jekyll - 在分配之前评估一个字符串