相同记录行如何取最大值
作者:德仔 来源:德仔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)
猜你喜欢
- 题目请设计并实现一款主机端口扫描程序。程序根据用户输入的域名或IP地址,可以查询该主机的开放的端口号。 例如:并有一定的异常处理参考code
- 本文实例讲述了JS求解三元一次方程组值的方法。分享给大家供大家参考,具体如下:// 求用js 码一段代码求 三元一次方程组的值!// a1
- 要更新的页面<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona
- 开发中偶尔需要判断网络的连通性,没有什么方法比 ping 更直接了当,通常检查网络情况都是运行命令ping www.baidu.com ,查
- pycharm下载库时出现Failed to install package怎么解决?奶奶都会解决的那种。一、当前现状 看看你是否也是这种情
- sql xml 入门: --by jinjazz 1、xml: 能认识元素、属性和值 2、xpath: 寻址语言,类似windows目录的查
- golang中的string是可以转换为byte数组或者rune数组但是其实byte对应的类型是uint8,而rune对应的数据类型就是in
- 前言本文主要给大家介绍了关于python指定时间调用函数的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:在前面的一
- 文件的io操作的缓冲行为分为全缓冲:同系统及磁盘块大小有关,n个字节后执行一次写入操作行缓冲:遇到换行符执行一次写操作无缓冲:立刻执行写操作
- 1、tensorflow(不定长)文本序列读取与解析tensorflow读取csv时需要指定各列的数据类型。但是对于RNN这种接受序列输入的
- 在Asp中如何得到所有表单的名称跟对应的值。其实,这个问题很简单,但是可能还是有很多人不知道该怎么做,所以特地写下来,仅供参考。在Asp程序
- 简单定义图轴:import numpy as npimport matplotlib.pyplot as plt创建一个简单的matplot
- 前言金融行业的Python学习,不同于IT系统开发,我们并不需要达到程序员的水平。然而,学会Python可以让你实现一个人写完一个交易系统的
- 本文实例讲述了JS创建对象的写法。分享给大家供大家参考,具体如下:写法1:<script>var database = func
- 需求针对tab键带来的多个空格问题,有时候我们针对带空格的一行数据要进行切割,如果有多个空格就会出现就会切割空格出现,我们想把空格都去掉,所
- 前言推导式提供了更简洁高效的方法来生成序列而又不失代码的可读性。定义: 推导式是 Python 里很有用的一个特性,它可以用一行代码就可以创
- 1 , javascript字符集:javascript采用的是Unicode字符集编码。为什么要采用这个编码呢?原因很简单,16位的Uni
- 一、requests库requests是使用Apache2 licensed 许可证的HTTP库。比urllib模块更简洁。Request支
- 本文为大家分享了WebStorm安装教程,供大家参考一、简介WebStorm 是jetbrains公司旗下一款JavaScript 开发工具
- 装饰器这东西我看了一会儿才明白,在函数外面套了一层函数,感觉和java里的aop功能很像;写了2个装饰器日志的例子,第一个是不带参数的装饰器