--测试环境 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 行)
select 订单表.* from 订单表 inner join (select 商品编码,min(单价) as 最小单价 from 订单表 group by 商品编码) as 最小单价表集 on 订单表.商品编码=最小单价表集.商品编码 and 订单表.单价=最小单价表集.最小单价订单表就是楼主那个数据表
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
select a.商品编码,a.数量,a.单价,a.供应商 from 表 a where not exists(select 1 from 表 where 商品编码=a.商品编码 and 单价<a.单价)
SELECT a.* FROM [table] a WHERE a.单价=(SELECT MIN(单价) FROM [table] b WHERE b.商品编码=a.商品编码)
兄弟这个行,我试过了 sno --商品编码 cnt --数量 mny --单价 pro --供应商 select sno,cnt,mny,pro from 表名 where mny in(select min(mny)from 表名 group by sno)
SELECT a.* FROM [table] a WHERE a.单价=(SELECT MIN(单价) FROM [table] b WHERE b.商品编码=a.商品编码) ------------- 如果最小单价有重复呢???
select 商品编码, 数量=(select top 1 数量 from @t where 商品编码=a.商品编码 order by 单价 ), 单价=(select top 1 单价 from @t where 商品编码=a.商品编码 order by 单价 ), 供应商=(select top 1 供应商 from @t where 商品编码=a.商品编码 order by 单价 ) from @t a group by 商品编码
同意 zlp321002的做法,也可用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 distinct A.* from @t A inner join ( select 商品编码,min(单价) 单价 from @t group by 商品编码 ) b on a.商品编码=b.商品编码 and a.单价=b.单价
Declare @t table(自动编号 int IDENTITY(1,1) not null ,商品编码 varchar(10),数量 int,单价 decimal(4,2),供应商 varchar(10)) declare @lookid varchar(10) set @lookid = '001' 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 '003',500,1.21,'SQ1企业' --查询 select b.* from @t as b inner join ( select 商品编码,min(单价) as 单价 from @t group by 商品编码) as C on b.商品编码 = @lookid and b.单价=c.单价这一个运行速度快,效率高,满足你的要求
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 行)
from 订单表 inner join
(select 商品编码,min(单价) as 最小单价
from 订单表
group by 商品编码) as 最小单价表集
on 订单表.商品编码=最小单价表集.商品编码 and 订单表.单价=最小单价表集.最小单价订单表就是楼主那个数据表
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
where not exists(select 1 from 表 where 商品编码=a.商品编码 and 单价<a.单价)
FROM [table] a
WHERE a.单价=(SELECT MIN(单价)
FROM [table] b
WHERE b.商品编码=a.商品编码)
sno --商品编码
cnt --数量
mny --单价
pro --供应商
select sno,cnt,mny,pro from 表名
where mny in(select min(mny)from 表名
group by sno)
:
商品编码 数量 单价 供应商
001 20 0.3 工商企业
001 50 0.1 AB企业
002 100 1.2 OK企业
003 200 2.4 AB企业
003 500 1.2 SQ企业
004 10 1.0 SQ企业
004 11 1.2 SQ企业sup_star(星雨) 的语句得到的结果就会是这样:商品编码 数量 单价 供应商
001 50 0.1 AB企业
002 100 1.2 OK企业
003 500 1.2 SQ企业
004 10 1.0 SQ企业
004 11 1.2 SQ企业
FROM [table] a
WHERE a.单价=(SELECT MIN(单价)
FROM [table] b
WHERE b.商品编码=a.商品编码)
-------------
如果最小单价有重复呢???
数量=(select top 1 数量 from @t where 商品编码=a.商品编码 order by 单价 ),
单价=(select top 1 单价 from @t where 商品编码=a.商品编码 order by 单价 ),
供应商=(select top 1 供应商 from @t where 商品编码=a.商品编码 order by 单价 )
from @t a
group by 商品编码
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 distinct A.*
from @t A
inner join (
select 商品编码,min(单价) 单价
from @t group by 商品编码
) b on a.商品编码=b.商品编码 and a.单价=b.单价
declare @lookid varchar(10)
set @lookid = '001'
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 '003',500,1.21,'SQ1企业'
--查询
select b.*
from @t as b
inner join
(
select 商品编码,min(单价) as 单价
from @t
group by 商品编码) as C
on b.商品编码 = @lookid and b.单价=c.单价这一个运行速度快,效率高,满足你的要求