我有一个为Powerbuilder编写的旧查询,我需要为SQL Azure Server更新它。
旧查询:
select distinct <a bunch of stuff>
from table1 ii,
table2 apc,
table3 ir,
table4 pt,
table5 cw,
table6 g,
table7 cd,
table8 cr,
table9 pr,
table10 tfl
where apc.production_coil_no = ii.production_coil_no
and apc.routing = pr.routing
and pr.product_type_cd = pt.product_type_cd
and apc.grade = g.grade
and ir.inventory_reason_cd = ii.inventory_reason_cd
and ir.pes_cd = pt.pes_cd
and tfl.to_freight_location_cd = apc.current_location
and pt.active = "Y"
and pr.active_flag = "Y"
and apc.coating_weight *= cw.coating_weight_cd
and apc.production_coil_no *= cd.production_coil_no
and cd.defect_cd *= cr.defect_cd
and cd.reason_for_last_status_change = 'Y'
and ii.inventory_reason_cd in ("IS","EP")
and (apc.snapshot in ("Y","P","E","S") or apc.sdi_produced = "N")
and ii.change_datetime <= dateadd(dd,-1,(case @customer_id when 11 then getdate()
when 16 then (case isnull(g.high_strength,"N") when "Y" then dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end)
when 639 then (case g.nm_high_strength when "Y" then getdate() end)
else (case isnull(g.high_strength,"N") when "Y" then dateadd(dd,-2 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end) end))
and ii.sfi_material_specification_cd in ("NONE","",null)
union
select distinct <a bunch of stuff>
from table1 ii,
table2 apc,
table3 pt,
table4 cw,
table5 g,
table6 cd,
table7 cr,
table8 mms,
table9 pr,
table10 tfl
where apc.production_coil_no = ii.production_coil_no
and apc.routing = pr.routing
and pr.product_type_cd = pt.product_type_cd
and apc.grade = g.grade
and ii.sfi_material_specification_cd = mms.material_specification_cd
and tfl.to_freight_location_cd = apc.current_location
and pt.pes_cd = "P"
and pt.active = "Y"
and pr.active_flag = "Y"
and apc.coating_weight *= cw.coating_weight_cd
and apc.production_coil_no *= cd.production_coil_no
and cd.defect_cd *= cr.defect_cd
and cd.reason_for_last_status_change = 'Y'
and ii.inventory_reason_cd in ("IS","EP")
and (apc.snapshot in ("Y","P","E","S") or apc.sdi_produced = "N")
and ii.change_datetime <= dateadd(dd,-1,(case @customer_id when 11 then getdate()
when 16 then (case isnull(g.high_strength,"N") when "Y" then dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end)
when 639 then (case g.nm_high_strength when "Y" then getdate() end)
else (case isnull(g.high_strength,"N") when "Y" then dateadd(dd,-2 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end) end))
and ii.sfi_material_specification_cd not in ("NONE","",null)
我试图重写这个,但我的测试数据目前不可靠,所以我不知道我是否写得正确。
第一次尝试:
select distinct <a bunch of stuff>
from table2 apc
JOIN table1 ii
ON apc.production_coil_no = ii.production_coil_no
JOIN table3 ir
ON ii.inventory_reason_cd = ir.inventory_reason_cd
JOIN table9 pr
ON apc.routing = pr.routing
JOIN table4 pt
ON pr.product_type_cd = pt.product_type_cd
AND ir.pes_cd = pt.pes_cd
JOIN table6 g
ON apc.grade = g.grade
JOIN table10 tfl /*might break here*/
ON apc.current_location = tfl.to_freight_location_cd
LEFT OUTER JOIN table5 cw
ON apc.coating_weight = cw.coating_weight_cd
LEFT OUTER JOIN table7 cd
ON apc.production_coil_no = cd.production_coil_no
LEFT OUTER JOIN table8 cr
ON cd.defect_cd = cr.defect_cd
where pt.active = 'Y'
and pr.active_flag = 'Y'
and cd.reason_for_last_status_change = 'Y'
and ii.inventory_reason_cd in ('IS','EP')
and (apc.snapshot in ('Y','P','E','S') or apc.sdi_produced = 'N')
and ii.change_datetime <= dateadd(dd,-1,(case @customer_id when 11 then getdate()
when 16 then (case isnull(g.high_strength,'N') when 'Y' then dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end)
when 639 then (case g.nm_high_strength when 'Y' then getdate() end)
else (case isnull(g.high_strength,'N') when 'Y' then dateadd(dd,-2 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end) end))
and ii.sfi_material_specification_cd in ('NONE',null)
union
select distinct <a bunch of stuff>
from table2 apc
JOIN table1 ii
ON apc.production_coil_no = ii.production_coil_no
JOIN table8 mms
ON ii.sfi_material_specification_cd = mms.material_specification_cd
JOIN table9 pr
ON apc.routing = pr.routing
JOIN table3 pt
ON pr.product_type_cd = pt.product_type_cd
JOIN table5 g
ON apc.grade = g.grade
JOIN table10 tfl /*might break here*/
ON apc.current_location = tfl.to_freight_location_cd
LEFT OUTER JOIN table4 cw
ON apc.coating_weight = cw.coating_weight_cd
LEFT OUTER JOIN table6 cd
ON apc.production_coil_no = cd.production_coil_no
LEFT OUTER JOIN table7 cr
ON cd.defect_cd = cr.defect_cd
where pt.pes_cd = 'P'
and pt.active = 'Y'
and pr.active_flag = 'Y'
and cd.reason_for_last_status_change = 'Y'
and ii.inventory_reason_cd in ('IS','EP')
and (apc.snapshot in ('Y','P','E','S') or apc.sdi_produced = 'N')
and ii.change_datetime <= dateadd(dd,-1,(case @customer_id when 11 then getdate()
when 16 then (case isnull(g.high_strength,'N') when 'Y' then dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end)
when 639 then (case g.nm_high_strength when 'Y' then getdate() end)
else (case isnull(g.high_strength,'N') when 'Y' then dateadd(dd,-2 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) else dateadd(dd,-1 + (case datepart(dw,getdate()) when 1 then -1 when 2 then -2 else 0 end),getdate()) end) end))
and ii.sfi_material_specification_cd not in ('NONE',null)
注意:我正在用我知道在测试时有效的测试值替换@customer_id。
我的问题是:我是否正确地重写了这个?如果没有,我该如何解决?