我的数据库里有
表1 table_Prodcution 产品表
产品 单位 数量
production unit quanty单位转换表
table_Convert
转换前单位 转换后单位
From_Unit To_Unit现在我想把产品及产品单位数量从数据库里取出来
要实现的效果是所产品的单位都是以第一条记录为基准
不同的进行转换 请问怎么实现
表1 table_Prodcution 产品表
产品 单位 数量
production unit quanty单位转换表
table_Convert
转换前单位 转换后单位
From_Unit To_Unit现在我想把产品及产品单位数量从数据库里取出来
要实现的效果是所产品的单位都是以第一条记录为基准
不同的进行转换 请问怎么实现
where unit<>a.From_Unit
From_Unit To_Unit Conversion
select production,
case when unit = (select top 1 unit from table_Prodcution where production = a.production) then unit else to_unit end as unit,
quanty
from table_Prodcution a left join table_Convert b on a.unit = b.From_Unit
select production,unit,sum(quanty) as quanty
from
(select production,
case when unit = (select top 1 unit from table_Prodcution where production = a.production) then unit else to_unit end as unit,
case when unit = (select top 1 unit from table_Prodcution where production = a.production) then quanty else quanty * Conversion end as quanty
from table_Prodcution a left join table_Convert b on a.unit = b.From_Unit
)tt
group by production,unit
如果是多个不同的产品呢 就会出错撒转换前单位 转换后单位 转换率
箱 本 100
箱 瓶 50产品 数量 单位
书 10 本
书 20 箱
饮料 20 瓶
饮料 15 箱结果产品 数量 单位
书 210.0 本
饮料 1500.0 本
书 100.0 瓶
饮料 770.0 瓶
select production,unit,sum(quanty) as quanty
from
(
select production,
case when from_unit is null then unit else to_unit end as unit,
case when from_unit is null then quanty else quanty * Conversion end as quanty
from table_Prodcution a left join table_Convert b
on a.unit = b.From_Unit and b.to_unit = (select top 1 unit from table_Prodcution where production = a.production)
)tt
group by production,unit--上面的写法注意几点:
--不支持三级单位的转换
--需要转换单位的记录,在单位转换表中必须能找到对应的转换记录
--单位转换表设计的不好!比如饮料是箱、瓶,假如再有蜂蜜,也是箱、瓶,而且转换率不同的话,那怎么处理?转换前单位 转换后单位 转换率
箱 本 100
箱 瓶 50 --这个是饮料的
箱 瓶 10 --这个是蜂蜜的
create table table_Prodcution
(
production varchar(10),
quanty int,
unit varchar(10)
)create table table_Convert
(
From_Unit varchar(10),
To_Unit varchar(10),
Conversion int
)insert into table_Prodcution select '书', 10, '本'
insert into table_Prodcution select '书', 2, '箱'
insert into table_Prodcution select '饮料',20, '瓶'
insert into table_Prodcution select '饮料',15, '箱'insert into table_Convert select '箱','本',100
insert into table_Convert select '箱','瓶',50--语句
select production,unit,sum(quanty) as quanty
from
(
select production,
case when from_unit is null then unit else to_unit end as unit,
case when from_unit is null then quanty else quanty * Conversion end as quanty
from table_Prodcution a left join table_Convert b
on a.unit = b.From_Unit and b.to_unit = (select top 1 unit from table_Prodcution where production = a.production)
)tt
group by production,unit
--结果
production unit quanty
---------- ---------- -----------
书 本 210
饮料 瓶 770(所影响的行数为 2 行)
SQL 语句太灵活了
学习不够啊
结帖
还有就是 我如果
想从 两张表里
create table table_Prodcution_1
(
production varchar(10),
quanty int,
unit varchar(10)
)create table table_Prodcution_2
(
production varchar(10),
quanty int,
unit varchar(10)
)查询产品出来
要求 每种产品以table_Prodcution_1的第一条记录的单位为单位
若table_Prodcution_2的表里的产品 不存在table_Prodcution_1中
则以table_Prodcution_2第一条记录的单位为单位
单位不同的话 都进行转换
满足上面效果的
该怎么做呢?