问个SQL语句
有一个表有如下字段和数据
caseid cvid enterdate enteroprid status
1 2 2005-01-02 77 07
1 2 2005-01-03 77 07
1 2 2005-01-02 78 07
1 3 2005-01-02 88 07
我只希望查询出一条 caseid cvid 是1 和2哪一条不限制和一条 1 和3的
前提是我要把这5个字段全查询出来,也就是只要caseid和cvid两个组合起来是唯一
我可以select distinct caseid,cvid from table但是得不到后面三个字段需要在次查询才能得到
我想用一句sql把这些满足的东西查询出来
问题点数:20、回复次数:9Top
1 楼happyflystone(无枪的狙击手)回复于 2006-03-08 17:09:32 得分 8
select *
from table a
where not exists(
select 1 from table where caseid = a.caseid and cvid = a.cvid and enteroprid > a.enteroprid )Top
2 楼yuweiwei(YWW(杨思))回复于 2006-03-08 17:12:23 得分 2
看不懂,你想要的结果是什么样的?Top
3 楼lingbo_wx(上海小浪人)回复于 2006-03-08 17:20:27 得分 0
2楼的语句不对啊
我想要的结果就是
caseid cvid两个出来的组合是唯一的
上面的例子出来应该是
1 2 X X X 3个X为那三条里面的任何一条只要一条
1 3 2005-01-02 88 07
Top
4 楼lsqkeke(可可)回复于 2006-03-08 17:20:50 得分 0
一楼的那样写,会有潜在重复的记录的!Top
5 楼lsqkeke(可可)回复于 2006-03-08 17:27:58 得分 8
declare @t table(caseid int, cvid int, enterdate varchar(20), enteroprid int ,status varchar(5))
insert @t
select 1 , 2 , '2005-01-02' , 77, '07' union all
select 1 , 2 , '2005-01-03' , 77, '07' union all
select 1 , 2 , '2005-01-02' , 78, '07' union all
select 1 , 3 , '2005-01-02' , 88, '07'
select id=identity(int,1,1),* into #t from @t
select caseid,cvid,enterdate,enteroprid,status from #t a
where id in(select top 1 id from #t where caseid = a.caseid and cvid = a.cvid)
drop table #tTop
6 楼lingbo_wx(上海小浪人)回复于 2006-03-08 17:31:20 得分 0
楼上的
select caseid,cvid,enterdate,enteroprid,status from #t a
where id in(select top 1 id from #t where caseid = a.caseid and cvid = a.cvid)
里面的id是什么字段?Top
7 楼zjdyzwx(十一月猪)回复于 2006-03-09 10:37:32 得分 2
SELECT caseid,cvid , MAX(enterdate ) ,MAX(enteroprid ),MAX(STATUS) FROM TABLE GROUP BY CASEID,CVIDTop
8 楼lingbo_wx(上海小浪人)回复于 2006-03-09 11:00:13 得分 0
好象都不是太对啊,enterdate enteroprid status这三个字段的数据是不确定的,不是按照什么顺序的,大小都不一定
我自己写了一个
select * from escanreport where rptid in (
select min(rptid) as rptid from escanreport a inner join
(
select distinct
caseid,
cvid
from escanreport
where status='07'
and status<>'20'
and caseid in(select caseid from case_info where statuscode2<>'10' and closedate>='2005-07-01')
)b
on a.caseid = b.caseid and a.cvid = b.cvid
group by a.caseid,a.cvid
) order by rptid
rptid是表里的自增列,唯一的
Top
9 楼lingbo_wx(上海小浪人)回复于 2006-03-09 11:01:59 得分 0
lsqkeke(可可)你的语句我加上条件会少很多记录,不知道为什么逻辑似乎是对的
happyflystone(没枪的狙击手) ( ) 的语句加上条件好象数据差不多
Top




