SQL Server一个特殊查询(http://searchdatabase.techtarget.com.cn/tips/206/2295706.shtml)
2007-06-22 16:27
1131 查看
我有一个问题,是关于SQL Server 查询的,求一查询语句商品编码唯一,同一商品取单价为最低一条记录。问题如下:
求一查询语句商品编码唯一,同一商品取单价为最低一条记录,结果如下:
回答一:
回答二:
商品编码 数量 单价 供应商 001 20 0.3 工商企业 001 50 0.1 AB企业 002 100 1.2 OK企业 003 200 2.4 AB企业 003 500 1.2 SQ企业 |
商品编码 数量 单价 供应商 001 50 0.1 AB企业 002 100 1.2 OK企业 003 500 1.2 SQ企业 |
--测试环境 Declare @t table(商品编码 varchar(10),数量 int,单价 decimal(4,2),供应商 varchar(10)) insert into @t select '001',20,0.3,'工商企业' union all select '001',50,0.1,'AB企业' union all select '002',100,1.2,'OK企业' union all select '003',200,2.4,'AB企业' union all select '003',500,1.2,'SQ企业' --查询 select * from @t A where not exists (select 1 from @t where 商品编码=A.商品编码 and 单价<A.单价) --结果 商品编码 数量 单价 供应商 ---------- ----------- ------ ---------- 001 50 .10 AB企业 002 100 1.20 OK企业 003 500 1.20 SQ企业 (所影响的行数为 3 行) |
Declare @t table(商品编码 varchar(10),数量 int,单价 decimal(4,2),供应商 varchar(10)) insert into @t select '001',20,0.3,'工商企业' union all select '001',50,0.1,'AB企业' union all select '002',100,1.2,'OK企业' union all select '003',200,2.4,'AB企业' union all select '003',500,1.2,'SQ企业' union all select '004',500,1.2,'SQ企业' union all select '003',500,1.2,'SQ企业' select a.* from @t a,(select 商品编码,min(单价) aaa from @t group by 商品编码)as aa where aa.商品编码=a.商品编码 and a.单价=aa.aaa |
相关文章推荐
- Ajax:拥抱JSON,让XML走开http://searchwebservices.techtarget.com.cn/tips/481/2708981_3.shtml
- SQL Server 2008的实用小道具——merger(转:http://database.ctocio.com.cn/18/11525518.shtml)
- [转]使用Excel分析MySQL数据[http://database.ctocio.com.cn/tips/145/7138145_3.shtml]
- http://www.techtarget.com.cn/
- 在jbuilder和netbeans中打jar包发布程序(http://tech.techweb.com.cn/thread-246441-1-1.html)
- DB2 Express-C 9.7.2 新增特性(转:http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1006db2expressc972/?S_CMP=d
- http://www.oracle.com/technetwork/cn/articles/hunter-rac11gr2-iscsi-3-092230-zhs.html#30
- Skinned Mesh 原理解析和一个最简单的实现示例 作者:n5 Email: happyfirecn##yahoo.com.cn Blog: http://blog.csdn.net/n5
- 一个简单的搜索应用程序(http://www.ibm.com/developerworks/cn/java/j-lo-lucene1/)
- 准备开发一个PSP专用的网站http://www.mypsp.com.cn
- IE兼容问题 (查询浏览器是否支持的网址http://caniuse.com/#search=transform)
- 这里的博客实在不方便,以后都在新浪也放一个博客。新浪http://blog.sina.com.cn/eggoo为主,这里作为镜像。
- 一个产品经理的自我总结:我常犯的一些毛病(from http://forum.eet-cn.com)
- 推荐一个提供Web Service的网站 http://www.webxml.com.cn
- 转载一个SAP下载工具的代码,仅用于学习(来源于:http://www.dalestech.com/)
- sql server分配某个用户只对某一个数据库有权限 转载 http://blog.sina.com.cn/s/blog_13554ebc70102wi3h.html
- opencv的一个神经网络例程(转自http://blog.sina.com.cn/s/blog_61e10f020101bv94.html)
- 转载一个SAP下载工具的代码,仅用于学习(来源于:http://www.dalestech.com/)
- http://www.cn-doc.com/_soft_dot_net_tech_info/2005_08_18_02/20050818020727495.htm
- 创建一个ASP通用分页类[转贴 http://www.blueidea.com/tech/program/2004/1989.asp]