34,594
社区成员
发帖
与我相关
我的任务
分享
--登记表-----------------------------------------
create table land
(
financialcode nchar(10),
dormantcode nchar(10),
sumcrotch int
)
-- 股份表--------------------------------------------------
create table stock
(
financialnumber nchar(10),
dormantnumber nchar(10),
sumget int
)
--测试数据----------------------------------------------------
insert into land
select '001','01',100 union all
select '002','02',100 union all
select '003','03',100 union all
select '004','04',100 union all
select '005','05',100 union all
select '006','06',100
insert into stock
select '001','01',100 union all
select '002','02',100 union all
select '003','03',100 union all
select '004','04',200
-----------------------------------------------------
select A.*,B.* from land A,stock B
where A.financialcode=B.financialnumber and
A.dormantcode=B.dormantnumber and
A.sumcrotch<>B.sumget
union
select *,'なし' financialnumber, 'なし'dormantnumber,0 sumget from land A
where not exists
(
select 1 from stock B where A.financialcode=B.financialnumber
)
-------------------------------------------------
/*
financialcode dormantcode sumcrotch financialnumber dormantnumber sumget
---------------------------------------------------------------------------
004 04 100 004 04 200
005 05 100 なし なし 0
006 06 100 なし なし 0
*/
select m.*
from 登记表 m , 股份表 n
where m.证券代码 = n.证券代码 and m.股东代码 = n.股东号 and m.总拥有股数 <> n.今持仓量
select m.* , n.*
from 登记表 m left join 股份表 n
on m.证券代码 = n.证券代码 and m.股东代码 = n.股东号 and m.总拥有股数 <> n.今持仓量
-->如果不等,就显示登记表中不等的记录来
select a.证券代码,a.股东代码,a.总拥有股数,b.今持仓量
from
(select 证券代码,股东代码,sum(总拥有股数) 总拥有股数 from 登记表 group by 证券代码,股东代码) a
left join
(select 证券代码,股东号,sum(今持仓量) 今持仓量 from 股份表 group by 证券代码,股东号) b
on a.证券代码=b.证券代码 and a.股东代码=b.股东代码
where a.总拥有股数<>b.今持仓量
select a.证券代码,a.股东代码,是否相等 = case when isnull(a.总拥有股数,0)=isnull(b.今持仓量,0) then '相等' else '不等' end
from
(select 证券代码,股东代码,sum(总拥有股数) 总拥有股数 from 登记表 group by 证券代码,股东代码) a
left join
(select 证券代码,股东号,sum(今持仓量) 今持仓量 from 登记表 group by 证券代码,股东号) b
on a.证券代码=b.证券代码 and a.股东代码=b.股东代码
select 登记表.* from 登记表,股份表 where 股份表.证券代码=登记表.证券代码
and 股份表.股东号=登记表.股东代码 and 股份表.今持仓量 <>登记表.总拥有股数
union
select * from 登记表 where 证券代码+股东代码 not in
(select 证券代码+股东代码 from 股份表)