最快的方法判断最小值

想飞的狼 2010-12-26 12:13:29
产品编号 价格A 价格B 价格C 价格D 价格E


价格为FLOAT,如何用最快的办法判断出价格A 价格B 价格C 价格D 价格E,中哪个价最低,字段名是什么,当价格为0的时候
不参加比较


...全文
170 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
想飞的狼 2010-12-27
  • 打赏
  • 举报
回复
如果两个价格重复,就会这样
产品编号 价格 价格名
001 1 价格A
001 1 价格B
如何让当两个价格名中的价格相同时,取靠前的价格名体现,比如A和B就取A,使产品编号是唯一的
xman_78tom 2010-12-27
  • 打赏
  • 举报
回复

select [产品编号],[价格名],[价格] into #temp
from (
select [产品编号],'价格A' [价格名],[价格A] [价格] from tb
union all
select [产品编号],'价格B' [价格名],[价格B] [价格] from tb
union all
select [产品编号],'价格C' [价格名],[价格C] [价格] from tb
union all
select [产品编号],'价格D' [价格名],[价格D] [价格] from tb
union all
select [产品编号],'价格E' [价格名],[价格E] [价格] from tb
) t
where [价格]<>0;

delete t from #temp t
where exists (select * from #temp
where t.[产品编号]=[产品编号] and t.[价格]>t.[价格]);

select *
from #temp t
where not exists (select * from #temp
where t.[产品编号]=[产品编号] and t.[价格名]>t.[价格名]);
billpu 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 josy 的回复:]

SQL code
select 产品编号,min(价格) as 最低价格
from
(
select 产品编号,价格A as 价格 from tb where 价格A!=0
union all
select 产品编号,价格B from tb where 价格B!=0
union all
select 产品编号,价格C from tb where 价格C!=0
union……
[/Quote]
up
飘零一叶 2010-12-26
  • 打赏
  • 举报
回复
SELECT id,MIN(VALUE) AS VALUE
FROM(
SELECT * FROM tb
UNPIVOT(VALUE FOR 价格 in([价格A],[价格B],[价格C],[价格D],[价格E])) as p) PP
WHERE VALUE<>0
group by id
百年树人 2010-12-26
  • 打赏
  • 举报
回复
select 产品编号,min(价格) as 最低价格
from
(
select 产品编号,价格A as 价格 from tb where 价格A!=0
union all
select 产品编号,价格B from tb where 价格B!=0
union all
select 产品编号,价格C from tb where 价格C!=0
union all
select 产品编号,价格D from tb where 价格D!=0
union all
select 产品编号,价格E from tb where 价格E!=0

) t
group by 产品编号

飘零一叶 2010-12-26
  • 打赏
  • 举报
回复
if OBJECT_ID('tb') is not null drop table tb
go
create table tb
(
id int,
价格A float,
价格B float,
价格C float,
价格D float,
价格E float
)
insert into tb
select 1,1.0,0,123,345,1.1

SELECT MIN(VALUE) AS VALUE
FROM(
SELECT * FROM tb
UNPIVOT(VALUE FOR 价格 in([价格A],[价格B],[价格C],[价格D],[价格E])) as p) PP
WHERE VALUE<>0
-------
VALUE
1
xman_78tom 2010-12-26
  • 打赏
  • 举报
回复

select * into #temp
from (
select [产品编号],'价格A' [价格名],[价格A] [价格] from tb
union all
select [产品编号],'价格B' [价格名],[价格B] [价格] from tb
union all
select [产品编号],'价格C' [价格名],[价格C] [价格] from tb
union all
select [产品编号],'价格D' [价格名],[价格D] [价格] from tb
union all
select [产品编号],'价格E' [价格名],[价格E] [价格] from tb
) t
where [价格]<>0;

select *
from #temp t
where not exists (select *
from #temp where t.[产品编号]=[产品编号] and t.[价格]>[价格]);
华夏小卒 2010-12-26
  • 打赏
  • 举报
回复
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (产品编号 varchar(1),价格A int,价格B int,价格C int,价格D int,价格E int)
insert into #tb
select 'a',1,2,3,6,3 union all
select 'b',5,2,4,2,1 union all
select 'c',2,1,4,3,5



select 产品编号,价格A as 价格 ,'价格A' as 价格名 into #t from #tb where 价格A!=0
union all
select 产品编号,价格B,'价格B' as 价格名 from #tb where 价格B!=0
union all
select 产品编号,价格C,'价格C' as 价格名 from #tb where 价格C!=0
union all
select 产品编号,价格D,'价格D' as 价格名 from #tb where 价格D!=0
union all
select 产品编号,价格E,'价格E' as 价格名 from #tb where 价格E!=0

select * from #t t1
where not exists(select * from #t where 产品编号=t1.产品编号 and 价格<t1.价格)

产品编号 价格 价格名
---- ----------- -----
a 1 价格A
c 1 价格B
b 1 价格E

(3 行受影响)


drop table #t



华夏小卒 2010-12-26
  • 打赏
  • 举报
回复
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (产品编号 varchar(1),价格A int,价格B int,价格C int,价格D int,价格E int)
insert into #tb
select 'a',1,2,3,6,3 union all
select 'b',5,2,4,2,1 union all
select 'c',2,1,4,3,5


;with cte as
(
select 产品编号,价格名,价格
from #tb
unpivot
(
价格 for 价格名 in(价格A,价格B,价格C,价格D,价格E)
)t
)
select * from cte t1
where not exists(select * from cte where 产品编号=t1.产品编号 and 价格<t1.价格)



产品编号 价格名 价格
---- -------------------------------------------------------------------------------------------------------------------------------- -----------
a 价格A 1
b 价格E 1
c 价格B 1

(3 行受影响)
想飞的狼 2010-12-26
  • 打赏
  • 举报
回复
谢谢各位高手回复,我用的是SQL2000,实在抱歉
xman_78tom 2010-12-26
  • 打赏
  • 举报
回复

;with t as (
select [产品编号],[价格名],[价格],
row_number() over (partition by [产品编号] order by [价格]) rn
from tb unpivot ([价格] for [价格名] in ([价格A],[价格B],[价格C],[价格D],[价格E])) upvt
where [价格]<>0
)
select [产品编号],[价格名],[价格] from t where rn=1;
想飞的狼 2010-12-26
  • 打赏
  • 举报
回复
字段名是什么怎么加进去呢
这样的结果


产品编号 最低价 价格名称
1300120201 1 价格E
1300141201 2 价格D
1300260105 3 价格B
1300750501 3 价格E
1303192201 2 价格C
1303720901 1 价格A


34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧