我的数据库里有
表1 table_Prodcution 产品表
产品           单位  数量
production     unit  quanty单位转换表
table_Convert
转换前单位  转换后单位
From_Unit   To_Unit现在我想把产品及产品单位数量从数据库里取出来
要实现的效果是所产品的单位都是以第一条记录为基准
不同的进行转换  请问怎么实现

解决方案 »

  1.   

    update  table_Prodcution set unit=a.To_Unit join table_Convert a on unit=a.From_Unit
    where unit<>a.From_Unit
      

  2.   

    不好意思 忘了 转换表有一个转换率了 Conversion
      

  3.   

    是每条产品的第一条转换前单位  转换后单位  转换率
    From_Unit   To_Unit    Conversion
      

  4.   


    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
      

  5.   


    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
      

  6.   

    TO  coolingpipe(冷箫轻笛)这样写 只能对一种产品实现这个效果
    如果是多个不同的产品呢 就会出错撒转换前单位  转换后单位  转换率
    箱            本        100
    箱            瓶        50产品  数量  单位
    书     10    本
    书     20    箱
    饮料   20    瓶
    饮料   15    箱结果产品  数量    单位
    书     210.0   本   
    饮料   1500.0  本
    书     100.0   瓶
    饮料   770.0   瓶
      

  7.   

    --这样写
    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           --这个是蜂蜜的
      

  8.   

    --环境
    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 行)
      

  9.   

    3Q 太感谢了 强人啊
    SQL 语句太灵活了 
    学习不够啊
    结帖
      

  10.   

    不能知道能不能给点学习 SQL的 建议
      

  11.   

    最后一个问题拉
    还有就是 我如果
    想从 两张表里
    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第一条记录的单位为单位
    单位不同的话 都进行转换
    满足上面效果的  
    该怎么做呢?