Sql 中 Exists 的应用

本文探讨了SQL查询中使用distinct关键字的选择性过滤,并通过连接不同数据源进行数据整合,重点介绍了如何通过子查询实现数据计数并筛选出重复项。内容涵盖了数据清理和聚合分析的关键步骤。

select distinct ttt.PRJ,ttt.CCTR    from (
select distinct 
 substring(gl06001,23,6)  CCTR, substring(gl06001,34,3)  PRJ--, '08' as Entity
from [cnbj1scala01].[ScalaDB_Trading].[dbo].GL060811
where substring(gl06001,34,3)!='' and substring(gl06001,23,6)!=''
--order by CCTR ,PRJ
union all

select distinct 
 substring(gl06001,23,6)  CCTR, substring(gl06001,34,3)  PRJ--, '03' as Entity
from [cnbj1scala01].[ScalaDB_Trading].[dbo].GL060311
where substring(gl06001,34,3)!='' and substring(gl06001,23,6)!=''
--order by CCTR ,PRJ 
) as ttt
where exists (

select tt.CCTR ,Count(tt.CCTR ) from (
 select distinct t.CCTR ,t.PRJ    from (
select distinct 
 substring(gl06001,23,6)  CCTR, substring(gl06001,34,3)  PRJ--, '08' as Entity
from [cnbj1scala01].[ScalaDB_Trading].[dbo].GL060811
where substring(gl06001,34,3)!='' and substring(gl06001,23,6)!=''
--order by CCTR ,PRJ
union all

select distinct 
 substring(gl06001,23,6)  CCTR, substring(gl06001,34,3)  PRJ--, '03' as Entity
from [cnbj1scala01].[ScalaDB_Trading].[dbo].GL060311
where substring(gl06001,34,3)!='' and substring(gl06001,23,6)!=''
--order by CCTR ,PRJ 
) as t 
) as tt
where ttt.CCTR =tt.CCTR
group by tt.CCTR
having COUNT(tt.CCTR )>=2
)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值