成都创新互联网站制作重庆分公司

sqlserver查交集,sqlserver 交叉查询

sql 查询统计部分有交集

sqlserver、oracle、db2下均可:

创新互联建站从2013年开始,先为颍泉等服务建站,颍泉等地企业,进行企业商务咨询服务。为颍泉企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

with t1(id,cardid,money,code,date) as (          

select 1,1, 500, 10001, '13-12-1' 

union all select 2,2, 400, 10001, '13-12-1' 

union all select 3,1, 400,     0, '13-12-1' 

union all select 4,1, 300,     0, '13-12-1' 

union all select 5,1, 500, 10001, '13-12-1' 

union all select 6,1, 600,     0, '13-12-1' 

), 

t2 as (select * from t1 where code!=0)

select t2.id, t2.cardid, t2.money, t2.code, t2.date, sum(t1.money) sum 

from t1 join t2 

on t1.cardid=t2.cardid and t1.id=t2.id 

and not exists(select 1 from t2 t3 

where t3.cardid = t2.cardid and t3.id=t1.id and t3.idt2.id) 

group by t2.id, t2.cardid, t2.money, t2.code, t2.date

sqlserver 两表交集之外的其中一表数据

只用数据某一字段进行子查询是行不通的,因为你有三个字段为联合主腱,如果acode=bcode并不能保证abbrq=bbbrq和ay=by.

正确的方法是左外连接,如果左表a有右表b所没有的任何记录,该记录右表b任何字段为空

select a.* from tableA a left join tableB b on a.acode=b.bcode and a.abbrq=b.bbbrq and a.ay=b.by where b.bcode is null

求多个表交集的SQL语句是什么呀???

使用 EXISTS 和 NOT EXISTS 查找交集与差集

使用 EXISTS 和 NOT EXISTS 引入的子查询可用于两种集合原理的操作:交集与差集。两个集合的交集包含同时属于两个原集合的所有元素。差集包含只属于两个集合中的第一个集合的元素。

city 列中 authors 和 publishers 的交集是作者和出版商共同居住的城市的集合。

USE pubs

SELECT DISTINCT city

FROM authors

WHERE EXISTS

(SELECT *

FROM publishers

WHERE authors.city = publishers.city)

下面是结果集:

city

--------

Berkeley

(1 row(s) affected)

当然,该查询可以写成一个简单的联接。

USE pubs

SELECT DISTINCT authors.city

FROM authors INNER JOIN publishers

ON authors.city = publishers.city

city 列中 authors 和 publishers 的差集是作者所居住的、但没有出版商居住的所有城市的集合,也就是除 Berkeley 以外的所有城市。

USE pubs

SELECT DISTINCT city

FROM authors

WHERE NOT EXISTS

(SELECT *

FROM publishers

WHERE authors.city = publishers.city)

该查询也可以写成:

USE pubs

SELECT DISTINCT city

FROM authors

WHERE city NOT IN

(SELECT city

FROM publishers)


分享标题:sqlserver查交集,sqlserver 交叉查询
地址分享:http://cxhlcq.com/article/hdihih.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部