select distinct * from (MainInfo as m inner join otherInfo as o on m.ContractNo=o.ContractNo and m.RegDate=o.RegDate and m.Refundtimes=o.Refundtimes)
inner join (select max(RegDate) as mregdate,ContractNo,Refundtimes from otherinfo group by ContractNo,Refundtimes ) as n on m.ContractNo=n.ContractNo and m.RegDate=n.mRegDate and m.Refundtimes=n.Refundtimes
where m.CardId='1111'
我这个是按你的条件写出来了。不能与你的要的结果相同,就是因为你的条件有问题。
其中有两条记录是同RegDate和CordID,不同refundtimes。你的条件没有针对这类问题进行处理。
你也提出:“碰到同一RegDate不同的refundtimes就不对了”。
另外,我觉得你的数据库结构不科学,两个表都有CardID字段,但内容不相同。还有就是两个表重复字段太多(三个),一般只要一个就够了。
inner join (select max(RegDate) as mregdate,ContractNo,Refundtimes from otherinfo group by ContractNo,Refundtimes ) as n on m.ContractNo=n.ContractNo and m.RegDate=n.mRegDate and m.Refundtimes=n.Refundtimes
where m.CardId='1111'
我这个是按你的条件写出来了。不能与你的要的结果相同,就是因为你的条件有问题。
其中有两条记录是同RegDate和CordID,不同refundtimes。你的条件没有针对这类问题进行处理。
你也提出:“碰到同一RegDate不同的refundtimes就不对了”。
另外,我觉得你的数据库结构不科学,两个表都有CardID字段,但内容不相同。还有就是两个表重复字段太多(三个),一般只要一个就够了。
解决方案 »
- 客户这样要求如何设计表呢?改列名与数据类型
- sql server 有命令能返回表结构吗?
- 怎么往sqlserver里输数据啊?
- 关于内联接查询的原理问题...查询顺序不太明白,谢谢解惑!
- 如何批量删除数据后更新原表???
- 怎么让“select name from syscolumns where id=object_id('table')”的排序和实际表的字段排序一样
- Sql2005分页存储过程报错
- 求句子,高手请出招!
- 简单问题,MS-SQL Server如何实现将一个表中的数据导至另一个表?1.如果表结构相同的话?2.如果表结构大致相同的话?
- 我在 查询分析器中使用 函数 FN_LISTEXTENDEDPROPERTY 获取扩展属性 可以成功,可是在delphi6里面却失败,为什么?
- 我的sql server运行程序老是占用cpu 95%左右,查看版本是Microsoft SQL Server 2000 - 8.00.760
- 关事SQL中事件查探器的一个小器
(select ContractNo, RegDate, Refundtimes=max(Refundtimes) from otherInfo
Group by ContractNo, RegDate,CardId ) b
on(a.ContractNo=b.ContractNo and a.RegDate=b.RegDate and a.Refundtimes=b.Refundtimes)
inner join maininfo c
on(a.ContractNo=c.ContractNo and a.RegDate=c.RegDate and a.Refundtimes=c.Refundtimes)
where c.cardid=1111
Create Table MainInfo(ContractNo varchar(5), RegDate datetime,Refundtimes int,CardId int)
insert into MainInfo
select '00001', '2004-01-01' , 1, 1111 union all
select '00001' , '2004-01-01' , 2 , 1111 union all
select '00002' ,'2005-02-01' , 1 , 1111 union all
select '00003' ,'2005-02-02', 1 , 2222
-------------the second table ------------------------------
create table otherInfo(ContractNo varchar(5), RegDate datetime, Refundtimes int, CardId varchar(20),Name varchar(12), ManageAddress varchar(20))
insert into otherInfo
select '00001', '2004-01-01' , 1, '332623800411477' , 'yhd0411', '浙江' union all
select '00001' , '2004-01-01', 1,'332623550129611' , '张三' , '浙江' union all
select '00001' , '2004-01-01', 1,'332603670318156' , '李四' , '北京' union all
select '00001' , '2004-01-01', 2,'332623800411477' , 'yhd0411', '浙江' union all
select '00001' ,'2004-01-01', 2,'332623550129611', '张三','浙江'
-------------------------下面實現 -------------------------------
select a.* from otherInfo a inner join
(select ContractNo, RegDate,cardid, Refundtimes=max(Refundtimes) from otherInfo
Group by ContractNo, RegDate,CardId ) b
on(a.ContractNo=b.ContractNo and a.RegDate=b.RegDate and a.Refundtimes=b.Refundtimes and a.cardid=b.cardid)
inner join maininfo c
on(a.ContractNo=c.ContractNo and a.RegDate=c.RegDate and a.Refundtimes=c.Refundtimes)
where c.cardid=1111
select a.cardid,a.name,a.ManageAddress,a.ContractNo,a.RegDate,a.Refundtimes from otherInfo a inner join
(select ContractNo, RegDate,cardid, Refundtimes=max(Refundtimes) from otherInfo
Group by ContractNo, RegDate,CardId ) b
on(a.ContractNo=b.ContractNo and a.RegDate=b.RegDate and a.Refundtimes=b.Refundtimes and a.cardid=b.cardid)
inner join maininfo c
on(a.ContractNo=c.ContractNo and a.RegDate=c.RegDate and a.Refundtimes=c.Refundtimes)
where c.cardid=1111
这样查找第一次好像很慢是不是要建立存储过程好点???