declare @pos nvarchar(15)  set @pos='PO06110002'
select qt_no as 采購批號,os_no as 采購單號,prd_no as 料號,prd_name as 品名,(select cus_no from mf_pos where os_no=@pos ) as 廠商
,qty as 采購數量,os_dd as 采購下單日,(select sum(qty) from  tf_ti where  ti_id='TI' and bat_no=tf_pos.bat_no and prd_no=tf_pos.prd_no  and bil_no=@pos  )  as 送檢總數, 
(select sum(qty_chk) from tf_ty where ty_id='YB' and bat_no=tf_pos.bat_no and prd_no=tf_pos.prd_no  and bil_no=@pos ) as 驗收退回
 , (select sum(qty) from  tf_pss  where ps_id='PB' and prd_no=tf_pos.prd_no and os_no in (select ps_no from tf_pss where os_no=@pos )    ) as 進貨退回
,  (采購數量-送檢總數+驗收退回+進貨退回)  as 未到數 
 from tf_pos where os_no=@pos這句有誤請問我訪怎麼改?
 (采購數量-送檢總數+驗收退回+進貨退回)  as 未到數 

解决方案 »

  1.   

    (采購數量-送檢總數+驗收退回+進貨退回)  as 未到數 
    --用as前面的语句分别替代
      

  2.   

    1.用原字段名代替
    qty-(select sum(qty) from  tf_ti where  ti_id='TI' and bat_no=tf_pos.bat_no and prd_no=tf_pos.prd_no  and bil_no=@pos  ) +
    (select sum(qty_chk) from tf_ty where ty_id='YB' and bat_no=tf_pos.bat_no and prd_no=tf_pos.prd_no  and bil_no=@pos ) +
    (select sum(qty) from  tf_pss  where ps_id='PB' and prd_no=tf_pos.prd_no and os_no in (select ps_no from tf_pss where os_no=@pos )    )2.先将前面部分写进临时表,这样最后一句:
     (采購數量-送檢總數+驗收退回+進貨退回)  as 未到數 
    就可以直接引用了