相同记录行如何取最大值

作者:德仔 来源:德仔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
投稿

猜你喜欢

  • UCenter 批量添加用户的php代码

    2024-05-11 09:25:09
  • PHP解决高并发问题(opcache)

    2023-11-07 23:17:49
  • Python自动化办公之Excel拆分与自动发邮件

    2022-05-03 09:05:40
  • PHP 中文处理技巧

    2024-05-11 09:45:09
  • Python实现将xml导入至excel

    2023-10-01 06:17:45
  • 修改mysql密码与忘记mysql密码的处理方法

    2024-01-23 21:53:48
  • 全面解析Python的While循环语句的使用方法

    2023-12-21 04:41:11
  • Python input()函数案例教程

    2021-12-21 15:45:02
  • python创建和使用字典实例详解

    2021-06-11 11:37:06
  • Python多线程获取返回值代码实例

    2023-03-25 17:42:27
  • 浅谈python中对于json写入txt文件的编码问题

    2022-01-28 05:08:58
  • Oracle基本PLSQL的使用实例详解

    2024-01-21 11:17:41
  • 访问和更改关系数据,使用MSSQL外联接

    2024-01-18 06:39:33
  • 在ASP中使用SQL语句之5:开始执行

    2007-08-11 12:36:00
  • Mysql如何查看表及字段信息

    2024-01-28 21:35:43
  • Python操作CSV格式文件的方法大全

    2023-07-08 18:57:45
  • Python异常继承关系和自定义异常实现代码实例

    2023-06-22 07:34:44
  • Python实现动态条形图的示例详解

    2021-05-18 13:40:38
  • SQL Server UPDATE语句的用法详解

    2024-01-17 06:39:23
  • 详解MySQL Workbench使用教程

    2024-01-27 04:37:55
  • asp之家 网络编程 m.aspxhome.com