最快的方法判断最小值

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


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


...全文
186 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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


昨日,11.19,最新整理了,第61-80题,现在公布上传。 另加上之前公布的第1-60 题,在此做一次汇总上传,以飨各位。 可以这么说,绝大部分的面试题,都是这100 道题系列的翻版, 此微软等公司数据结构+算法面试100 题系列,是极具代表性的经典面试题。 而,对你更重要的是,我自个还提供了答案下载,提供思路,呵。 所以,这份资料+答案,在网上是独一无二的。 ------------------------------------ 整理资源,下载地址: 答案系列: 1.[最新答案V0.3 版]微软等数据结构+算法面试100 题[第21-40 题答案] http://download.csdn.net/source/2832862 2.[答案V0.2 版]精选微软数据结构+算法面试100 题[前20 题]--修正 http://download.csdn.net/source/2813890 //此份答案是针对最初的V0.1 版本,进行的校正与修正。 3.[答案V0.1 版]精选微软数据结构+算法面试100 题[前25 题] http://download.csdn.net/source/2796735 题目系列: 4.[第一部分]精选微软等公司数据结构+算法经典面试100 题[1-40 题] http://download.csdn.net/source/2778852 5.[第1 题-60 题汇总]微软等数据结构+算法面试100 题 http://download.csdn.net/source/2826690 更多资源,下载地址: http://v_july_v.download.csdn.net/ 若你对以上任何题目或任何答案,有任何问题,欢迎联系我: My E-mail: zhoulei0907@yahoo.cn ------------- 作者声明: 本人July 对以上公布的所有任何题目或资源享有版权。转载以上公布的任何一题, 或上传百度文库资源,请注明出处,及作者我本人。 向你的厚道致敬。谢谢。 ---July、2010 年11 月20 日。 ------------------------------------------------------ 各位,若对以上100题任何一道,或对已上传的任何一题的答案, 有任何问题,请把你的思路、想法,回复到此帖子上, 微软等100题系列,永久维护地址(2010年11.26日): http://topic.csdn.net/u/20101126/10/b4f12a00-6280-492f-b785-cb6835a63dc9.html

34,871

社区成员

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

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