相同记录行如何取最大值

作者:德仔 来源:德仔Blog 时间:2008-07-26 12:32:00 

相同记录行如何取最大值

我想这个东西在作一些相关采购系统或成本报价系统应该很有用的吧

取当前的最有效的价格.记录下来

与大家分享!

--测试数据   
  create   table   #表A   (fid   int,fprice   dec(5,2),fsuid   int,fdate   datetime)   
  insert   #表A   select   1,           2.1,             2,               '2005-09-09'   
  insert   #表A   select   1,           2.2,             2,               '2005-10-10'   
  insert   #表A   select   1,           3.3,             2,               '2006-01-01'   
  insert   #表A   select   2,           3.3,             1,               '2003-09-09'   
  insert   #表A   select   2,           5.5,             1,               '2005-09-09'   
  insert   #表A   select   2,           3.2,             2,               '2005-09-09'   
  insert   #表A   select   2,           5.5,             2,               '2005-09-15'   
    
  --查询   
  select   distinct   a.*   from   #表A   a,(select   fid,fsuid,max(fdate)   as   'fdate'   from   #表A   group   by   fid,fsuid)   b   
  where   a.fid=b.fid   and   a.fsuid=b.fsuid   and   a.fdate=b.fdate   order   by   a.fid   
  /*结果   
  fid                   fprice     fsuid               fdate                                                                                                       
  -----------   -------   -----------   ------------------------------------------------------     
  1                       3.30         2                       2006-01-01   00:00:00.000   
  2                       5.50         1                       2005-09-09   00:00:00.000   
  2                       5.50         2                       2005-09-15   00:00:00.000   
    
  (所影响的行数为   3   行)
  
  
  求是最大日期对应的价格
  
  --Try   
    
  --测试数据   
  create   table   #A   (fid   int,fprice   dec(5,2),fsuid   int,fdate   datetime)   
  insert   #A   select   1,           2.1,             2,               '2005-09-09'   
  insert   #A   select   1,           2.2,             2,               '2005-10-10'   
  insert   #A   select   1,           3.3,             2,               '2006-01-01'   
  insert   #A   select   2,           3.3,             1,               '2003-09-09'   
  insert   #A   select   2,           5.5,             1,               '2005-09-09'   
  insert   #A   select   2,           3.2,             2,               '2005-09-09'   
  insert   #A   select   2,           5.5,             2,               '2005-09-15'   
    
  select   *   from   #A   a     
  where   not   exists   
  (select   1   from   #A   where   fid=a.fid   and   fsuid=a.fsuid   and   fdate>a.fdate)


3.取单条价格最大记录

 select   price,materialcode   from   UBS_CAS_PriceForm   a     
  where  materialcode='1140099' and  not   exists   
  (select   1   from   UBS_CAS_PriceForm   where   materialcode=a.materialcode  and     pubdate>a.pubdate)

标签:记录,sql,数据,最大值
0
投稿

猜你喜欢

  • 动态SQL中返回数值的实现代码

    2012-01-05 18:53:54
  • 用户分类浅谈

    2009-09-27 12:14:00
  • MySQL Group By用法

    2012-01-05 19:05:14
  • MySQL的添加用户的两种方法

    2012-01-29 17:50:10
  • 分析Silverlight Button控件布局

    2009-02-17 13:13:00
  • 如何在js中使用FileSystemObject(fso)

    2007-09-23 09:10:00
  • 用户研究中的人物角色

    2010-02-08 12:15:00
  • asp当中判断函数一览

    2010-05-27 12:15:00
  • 浅谈购物类网站如何保持视觉设计的一致性

    2009-03-30 16:02:00
  • SQL Server 2005 输入框不能输入中文问题

    2010-02-04 09:14:00
  • 使用cmd命令行窗口操作SqlServer的方法

    2012-07-21 14:24:06
  • jQuery 1.3.3 新功能[译]

    2009-06-04 12:23:00
  • 利用XMLHTTP检测网址及探测服务器类型

    2009-04-24 15:12:00
  • Christopher Schmitt 谈学习CSS的益处

    2008-07-13 14:15:00
  • 三种禁用FileSystemObject组件的方法

    2007-09-23 15:52:00
  • ASP实例:幻灯片新闻代码

    2008-11-21 17:40:00
  • 函数式JavaScript编程指南

    2007-12-08 20:39:00
  • 巧用Dreamweaver4文件库更新网站

    2007-02-03 11:31:00
  • 如何随机显示图片计数器?

    2010-05-16 15:21:00
  • asp实现在线人数统计代码

    2008-08-10 18:35:00
  • asp之家 网络编程 m.aspxhome.com