if object_id(‘temp1’) is not null
drop table temp1
if object_id(‘temp2’) is not null
drop table temp2
select prd_no,prd_name,ps_dd,ps_no,up,TAX_RTO into temp1 from (select prd_no,prd_name,ps_dd,ps_no,up,TAX_RTO,row_number() over (partition by prd_no order by prd_no,ps_dd desc)as num from tf_pss where ps_id=‘PC’) t where num=1 order by prd_no
select a.*,b.cus_no,b.tax_id into temp2 from temp1 a left join mf_pss b on a.ps_no=b.ps_no
select a.prd_no as 物料编号,a.prd_name as 物料名称,a.ps_dd as 进货日期,a.ps_no as 进货单号,a.up as 单价,(CASE WHEN a.tax_id=‘1’ THEN ‘不计税’ WHEN a.tax_id=‘2’ THEN ‘应税内含’ WHEN a.tax_id=‘3’ THEN ‘应税外加’ ELSE ‘’ end) as 扣税类别,a.tax_rto as 税率,(case when convert(int,a.tax_id)=3 then a.up*(1+a.tax_rto/100) when convert(int,a.tax_id)=2 then a.up else 0 end) 含税单价
,a.cus_no as 供应商代号,b.name as 供应商名称 from temp2 a left join cust b on a.cus_no=b.cus_no order by a.prd_no