惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
L
LangChain Blog
J
Java Code Geeks
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
I
InfoQ
博客园 - 聂微东
量子位
A
About on SuperTechFans
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
H
Help Net Security

博客园 - Steven.zhou

Setting Up Database Mirroring Using Certificates get the server's infomation sql 2005数据库加密实战 Raid(0,1,5,10,01) SQL Server 2005高可用性之镜像功能(http://tech.it168.com/db/s/2007-04-24/200704240837593.shtml) SQL Server 2005高可用性之复制(http://tech.it168.com/db/s/2007-05-15/200705150909375.shtml) SQL Server 2005高可用性之日志传送(http://tech.it168.com/db/s/2007-06-29/200706291229796_2.shtml) SQL Server 2005性能测试实践(读书笔记翻译http://www.testage.net/TestTech/PT/200702/1510.htm) Troubleshooting the Performance of a SQL Server Solution(读书笔记,全是copy原文的) 如何缩小SQL Server数据库日志文件 sql server 2005几个好用易忘的功能 在SSIS中使用自定义的DLL文件 查看数据表的大小 Use Table-Valued Functions as Arrays in SQL Serve 在sql server 中如何移动tempdb到新的位置 动态sql语句返回值 得到所有的触发器(含DDL触发器) 得到数据库中所有用户表的字段、数据类型 在sql2005 中Execute的增强
SQL Server一个特殊查询(http://searchdatabase.techtarget.c...
Steven.zhou · 2007-06-22 · via 博客园 - Steven.zhou

 我有一个问题,是关于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