CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

问个SQL语句

楼主lingbo_wx(上海小浪人)2006-03-08 16:59:31 在 MS-SQL Server / 基础类 提问

有一个表有如下字段和数据  
  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

相关问题

  • 问个SQL语句。
  • 问个SQL语句?
  • 问个SQL语句
  • 问个sql语句
  • 问个sql语句
  • 一个sql语句问题
  • 问一个SQL语句
  • 请问一个SQL语句
  • 一个SQL语句问题
  • 问一个sql语句

关键词

  • 语句
  • 字段
  • 查询
  • 数据
  • caseid
  • cvid
  • enteroprid
  • enterdate
  • escanreport
  • rptid

得分解答快速导航

  • 帖主:lingbo_wx
  • happyflystone
  • yuweiwei
  • lsqkeke
  • zjdyzwx

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo