产品编号 价格A 价格B 价格C 价格D 价格E
价格为FLOAT,如何用最快的办法判断出价格A 价格B 价格C 价格D 价格E,中哪个价最低,字段名是什么,当价格为0的时候
不参加比较
价格为FLOAT,如何用最快的办法判断出价格A 价格B 价格C 价格D 价格E,中哪个价最低,字段名是什么,当价格为0的时候
不参加比较
解决方案 »
- 初学SQL 2008者的一个问题,急急急!
- 主从表操作时,主表插入数据后,主键为函数自动生成的流水号,如何取得它的主键值
- 为什么微软建议用 TOP N 来代替 SET ROWCOUNT ?
- 表中有一日期字段,是日期+时间格式
- [新手]如何将较长的文本录入到数据库
- 急问:丢失的记录如何补回?
- sql 判断列中数据后再查询
- 急!急!急!为什么我在win2000专业版上安装sql(个人版与开发版均如此)时提示安装程序配置服务器失败???
- 急!请问哪位高手知道在MSSQL中怎样把几个字段作为一个主键啊,HELP!!!!
- 关于返回表名的问题
- 查找局域网内sql Server服务器
- sql server managment studio 2008 没有响应
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.1SELECT 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
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 产品编号
FROM(
SELECT * FROM tb
UNPIVOT(VALUE FOR 价格 in([价格A],[价格B],[价格C],[价格D],[价格E])) as p) PP
WHERE VALUE<>0
group by id
这样的结果
产品编号 最低价 价格名称
1300120201 1 价格E
1300141201 2 价格D
1300260105 3 价格B
1300750501 3 价格E
1303192201 2 价格C
1303720901 1 价格A
;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;
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 行受影响)
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
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.[价格]>[价格]);
产品编号 价格 价格名
001 1 价格A
001 1 价格B
如何让当两个价格名中的价格相同时,取靠前的价格名体现,比如A和B就取A,使产品编号是唯一的
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.[价格名]);