一个有关单位换算更新表的问题
有两张表的内容分别为:select * from table1value unit
------ -------
136.29 兆瓦
619.33 千瓦
17305870.00 瓦特
56290.00 瓦特
2356.00 千瓦select * from table2unit1 unit2 rate
----- ----- ----
瓦特 千瓦 0.001
兆瓦 千瓦 1000
其中table1为数值表,但是单位不统一,他需要根据table2中的换算关系来换算成统一单位。
比如后面是“兆瓦”单位的,前面的数值就乘以1000,如果是“瓦特”的就乘以0.001(即除以1000)
想要得到的结果为:value unit
------ -------
136000.29 千瓦
619.33 千瓦
17305.87 千瓦
56.29 千瓦
2356.00 千瓦求SQL语句,越简单越好
有两张表的内容分别为:select * from table1value unit
------ -------
136.29 兆瓦
619.33 千瓦
17305870.00 瓦特
56290.00 瓦特
2356.00 千瓦select * from table2unit1 unit2 rate
----- ----- ----
瓦特 千瓦 0.001
兆瓦 千瓦 1000
其中table1为数值表,但是单位不统一,他需要根据table2中的换算关系来换算成统一单位。
比如后面是“兆瓦”单位的,前面的数值就乘以1000,如果是“瓦特”的就乘以0.001(即除以1000)
想要得到的结果为:value unit
------ -------
136000.29 千瓦
619.33 千瓦
17305.87 千瓦
56.29 千瓦
2356.00 千瓦求SQL语句,越简单越好
a.[value]*b.[rate] as [value],b.unit
from
a,b
where
a.unit1=b.unit1
if object_id('[table1]') is not null drop table [table1]
create table [table1]([value] numeric(10,2),[unit] varchar(4))
go
insert [table1]
select 136.29,'兆瓦' union all
select 619.33,'千瓦' union all
select 17305870.00,'瓦特' union all
select 56290.00,'瓦特' union all
select 2356.00,'千瓦'
--> 测试数据:[table2]
if object_id('[table2]') is not null drop table [table2]
create table [table2]([unit1] varchar(4),[unit2] varchar(4),[rate] numeric(7,3))
go
insert [table2]
select '瓦特','千瓦',0.001 union all
select '兆瓦','千瓦',1000select
t.value*isnull(r.rate,1) as value,
isnull(r.unit2,t.unit)
from [table1] t left join [table2] r
on t.unit = r.unit1
value
-------------------- ----
136290.00000 千瓦
619.33000 千瓦
17305.87000 千瓦
56.29000 千瓦
2356.00000 千瓦(所影响的行数为 5 行)
千瓦 千瓦 1select t1.value*t2.rate,t2.unit2 from t1,t2
where t1.unit=t2.unit1
-- Author :SQL77(只为思齐老)
-- Date :2010-03-09 14:59:46
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([value] numeric(10,2),[unit] varchar(4))
insert #TB
select 136.29,'兆瓦' union all
select 619.33,'千瓦' union all
select 17305870.00,'瓦特' union all
select 56290.00,'瓦特' union all
select 2356.00,'千瓦'
--> 测试数据:#TB1
if object_id('tempdb.dbo.#TB1') is not null drop table #TB1
go
create table #TB1([unit1] varchar(4),[unit2] varchar(4),[rate] numeric(7,3))
insert #TB1
select '瓦特','千瓦',0.001 union all
select '兆瓦','千瓦',1000
--------------开始查询--------------------------
UPDATE T SET VALUE=VALUE*T1.RATE,unit='千瓦' FROM #TB T,#TB1 T1 WHERE T.unit=T1.unit1
select * from #TB----------------结果----------------------------
/* (所影响的行数为 5 行)
(所影响的行数为 2 行)
(所影响的行数为 3 行)value unit
------------ ----
136290.00 千瓦
619.33 千瓦
17305.87 千瓦
56.29 千瓦
2356.00 千瓦(所影响的行数为 5 行)
*/
----- ----- ----
瓦特 千瓦 0.001
兆瓦 千瓦 1000
千瓦 瓦特 1000
兆瓦 瓦特 1000000
。 。 。
。 。 。
select
a.[value]*b.[rate] as [value],'千瓦' as unit
from
a,b
where
a.unit1=b.unit1
a.[value]*b.[rate] as [value],b.unit2
from
a left join b
on
a.unit1=b.unit1
if object_id('proc_trans_changeunit') is not null drop proc proc_trans_changeunit
go
create proc proc_trans_changeunit @unit varchar(20)
asupdate t
set value = value*(select rate from table2 where unit1 = t.unit and unit2 = @unit)
from table1 t
where unit != @unit
go--执行
exec proc_trans_changeunit '千瓦'
--查询
select * from table1
value unit
------------ ----
136290.00 兆瓦
619.33 千瓦
17305.87 瓦特
56.29 瓦特
2356.00 千瓦
as
begin
select a.[value]*b.[rate] as [value],b.unit2
from a left join b
on a.unit1=b.unit1 and b.unit2=@uu
end
as
begin
select a.[value]*b.[rate] as [value],b.unit2
from a left join b
on a.unit1=b.unit1 and b.unit2=@uu
end