销售订单
产品名称 币别 价格
产品1 人民币 500
产品2 人民币 700
产品3 美元 30
产品4 美元 50
产品4 美元 50价格政策
产品名称 币别 价格
产品1 人民币 450
产品4 美元 60
产品5 人民币 300
产品5 人民币 320
产品6 人民币 400
产品6 美元 50产品入库
产品名称 入库数量
产品1 10
产品2 50
产品3 150
产品4 6
产品5 90
产品6 85 按产品入库查询,价格首先选择销售订单,如不存在则选择价格政策,1美元拆人民币=1*6.83*1.17,
销售订单和价格政策的价格如有两条以上按平均值。达到查询结果如下:

产品名称 入库数量 价格 产值
产品1 10 500 5000.00 
产品2 50 700 35000.00 
产品3 150 239.73 35959.50 
产品4 6 399.56 2397.36 
产品5 90 310 27900.00 
产品6 85 399.78 33981.30 
合计 391 140238.16 本人刚学,自己做不出来,求教诸位,谢谢!!!

解决方案 »

  1.   

    case when  ..then .. else ..end 
      

  2.   

    select [产品入库单].[ffullnumber] as [物料代码],[产品入库单].[fitemname] as [物料名称],[产品入库单].[fitemmodel] as [规格型号],[产品入库单].[funitidname] as [单位],[产品入库单].[fauxqty] as [数量],
    cast(case when [销售订单分录表].[fauxpricediscount]<>0 or [销售订单分录表].[fauxpricediscount] is not null then case [销售订单表].fcurrencyid when 1 then [销售订单分录表].[fauxpricediscount]
               when 1000 then case 
    when substring([产品入库单].[ffullnumber],1,4)='4.01' then [销售订单分录表].[fauxpricediscount]*6.83*1.17
    when substring([产品入库单].[ffullnumber],1,4)='4.02' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    when substring([产品入库单].[ffullnumber],1,4)='4.03' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    when substring([产品入库单].[ffullnumber],1,4)='4.04' then [销售订单分录表].[fauxpricediscount]*6.83*1.17
    when substring([产品入库单].[ffullnumber],1,4)='4.05' then [销售订单分录表].[fauxpricediscount]*6.83*1.17
    when substring([产品入库单].[ffullnumber],1,4)='4.06' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    when substring([产品入库单].[ffullnumber],1,4)='4.07' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    end
    else 0
           end 
    when [销售订单分录表].[fauxpricediscount] is null or [销售订单分录表].[fauxpricediscount]=0 then [价格政策分录表].[fprice]
    end as money) AS [含税单价],
    cast(([产品入库单].[fauxqty])*case when [销售订单分录表].[fauxpricediscount]<>0 or [销售订单分录表].[fauxpricediscount] is not null then case [销售订单表].fcurrencyid when 1 then [销售订单分录表].[fauxpricediscount]
               when 1000 then case 
    when substring([产品入库单].[ffullnumber],1,4)='4.01' then [销售订单分录表].[fauxpricediscount]*6.83*1.17
    when substring([产品入库单].[ffullnumber],1,4)='4.02' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    when substring([产品入库单].[ffullnumber],1,4)='4.03' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    when substring([产品入库单].[ffullnumber],1,4)='4.04' then [销售订单分录表].[fauxpricediscount]*6.83*1.17
    when substring([产品入库单].[ffullnumber],1,4)='4.05' then [销售订单分录表].[fauxpricediscount]*6.83*1.17
    when substring([产品入库单].[ffullnumber],1,4)='4.06' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    when substring([产品入库单].[ffullnumber],1,4)='4.07' then [销售订单分录表].[fauxpricediscount]*6.83*1.15
    end
    else 0
           end 
    when [销售订单分录表].[fauxpricediscount] is null or [销售订单分录表].[fauxpricediscount]=0 then [价格政策分录表].[fprice]
    end  as money) as [产值],
    ([生产任务单].[FPlanFinishDate]) AS [计划完工日期]
    from vwicbill_2 as [产品入库单]
    left join [ICMO] AS [生产任务单] ON [产品入库单].[ficmobillno]=[生产任务单].[fbillno]
    left join [seorderentry] AS [销售订单分录表] ON ([销售订单分录表].[FInterID]=[生产任务单].[forderinterid] and [销售订单分录表].[FitemID]=[生产任务单].[FItemID])
    left join [seorder] AS [销售订单表] ON [销售订单分录表].[finterid]=[销售订单表].[finterid]
    left join [t_icitem] as [物料表] on [物料表].[fnumber]=[产品入库单].[ffullnumber]
    left join [icprcplyentry] as [价格政策分录表] on [价格政策分录表].[fitemid]=[物料表].[fitemid]  
    WHERE 1=1
    AND [产品入库单].FDate>='********'
    AND [产品入库单].FDate<='########'
    AND [产品入库单].ffullnumber>='*ItemNo*'
    AND [产品入库单].ffullnumber<='#ItemNo#'
    ORDER BY [产品入库单].ffullnumber
     
     这是实例,但对于销售订单和价格政策里有两条以上的我无法归并!
      

  3.   


    declare @ta table(产品名称 varchar(10), 币别 varchar(10),价格 decimal(18,2))
    insert @ta select  
    '产品1', '人民币' ,500 union all select 
    '产品2', '人民币' ,700 union all select
    '产品3', '美元' ,30 union all select
    '产品4', '美元' ,50 union all select
    '产品4', '美元', 50 declare @tb table(产品名称 varchar(10), 币别 varchar(10),价格 decimal(18,2))
    insert @tb select   
    '产品1', '人民币' ,450  union all select
    '产品4' ,'美元' ,60 union all select
    '产品5', '人民币' ,300 union all select
    '产品5' ,'人民币' ,320 union all select
    '产品6' ,'人民币', 400 union all select
    '产品6', '美元', 50 
    declare @tc table(产品名称 varchar(10),入库数量 int)
    insert @tc select  
    '产品1', 10 union all select
    '产品2' ,50 union all select 
    '产品3' ,150 union all select
    '产品4' ,6 union all select
    '产品5' ,90 union all select
    '产品6' ,85
    /*
    按产品入库查询,价格首先选择销售订单,如不存在则选择价格政策,1美元拆人民币=1*6.83*1.17, 
    销售订单和价格政策的价格如有两条以上按平均值。达到查询结果如下: 产品名称 入库数量 价格 产值 
    产品1 10 500 5000.00 
    产品2 50 700 35000.00 
    产品3 150 239.73 35959.50 
    产品4 6 399.56 2397.36 
    产品5 90 310 27900.00 
    产品6 85 399.78 33981.30 
    合计 391 140238.16 */select 产品名称=case when grouping(c.产品名称)=1 then '合计' else c.产品名称 end,入库数量=sum(入库数量),价格=case when grouping(c.产品名称)=1 then 0 else max(cast(isnull(a.价格,b.价格)as decimal(18,2))) end,
    产值=sum(cast(isnull(a.价格,b.价格)as decimal(18,2))*入库数量)
    from @tc c
    left join 
    (select  产品名称,价格=avg(case when 币别= '人民币' then 价格 else 价格*6.83*1.17 end )
    from @ta
    group by 产品名称
    ) a  on c.产品名称=a.产品名称
    left join 
    (select  产品名称,价格=avg(case when 币别= '人民币' then 价格 else 价格*6.83*1.17 end )
    from @tb
    group by 产品名称
    ) b
     on c.产品名称=b.产品名称
    group by c.产品名称
    with rollup
    产品名称       入库数量        价格                                      产值
    ---------- ----------- --------------------------------------- ---------------------------------------
    产品1        10          500.00                                  5000.00
    产品2        50          700.00                                  35000.00
    产品3        150         239.73                                  35959.50
    产品4        6           399.56                                  2397.36
    产品5        90          310.00                                  27900.00
    产品6        85          399.78                                  33981.30
    合计         391         0.00                                    140238.16