列名ItemA
,ItemB
,ItemC
,ItemD
和ItemE
我正在将其从Progress迁移到SQL,并且无法引用查询中的计算字段。在研究错误之后 似乎唯一的解决方案是用完整表达式替换每个项目或创建嵌套的select语句。在这种情况下似乎都不起作用。
SELECT
OrderQty - (ItemE + ShippedQty) AS ItemA,
((CASE WHEN (ItemA > ItemB AND ItemA > 0)
THEN (ItemA - ItemB)
ELSE 0
END)) AS ItemC,
UnitPrice * ItemC AS ItemD,
((CASE WHEN OnHandQty > (ReservedQty + PickingQty)
THEN OnHandQty - (ReservedQty + PickingQty)
ELSE 0
END)) AS ItemB,
((CASE WHEN ReservedQty > - 1
THEN (ReservedQty + PickedQty + PickingQty)
ELSE 0
END)) AS ItemE,
-- Example of enumerated expression that is far to confusing to continue.
SELECT
OrderQty - (((CASE WHEN ReservedQty > -1 THEN (ReservedQty + PickedQty + PickingQty) ELSE 0 END)) + ShippedQty),
((CASE WHEN (OrderQty - ((CASE WHEN ReservedQty > -1 THEN (ReservedQty + PickedQty + PickingQty) ELSE 0 END)) >
((CASE WHEN OnHandQty > (ReservedQty + PickingQty) THEN OnHandQty - (ReservedQty + PickingQty) ELSE 0 END)) AND
(OrderQty - (ItemE + ShippedQty) > 0) THEN (OrderQty - (ItemE + ShippedQty) ELSE 0 END)))),
UnitPrice * ((CASE WHEN (ItemA > ItemB AND ItemA > 0) THEN (ItemA - ItemB) ELSE 0 END))...
答案 0 :(得分:2)
如果要迁移到SQL Server,可以使用CROSS APPLY
子句为每个计算字段创建别名:
SELECT
x.ItemA,
t.ItemC,
UnitPrice * t.ItemC AS ItemD,
y.ItemB,
z.ItemE
FROM mytable
CROSS APPLY (SELECT CASE WHEN ReservedQty > - 1
THEN (ReservedQty + PickedQty + PickingQty)
ELSE 0
END) z(ItemE)
CROSS APPLY (SELECT OrderQty - (ItemE + ShippedQty)) x(ItemA)
CROSS APPLY (SELECT CASE WHEN OnHandQty > (ReservedQty + PickingQty)
THEN OnHandQty - (ReservedQty + PickingQty)
ELSE 0
END) y(ItemB)
CROSS APPLY (SELECT CASE WHEN (x.ItemA > y.ItemB AND x.ItemA > 0)
THEN (x.ItemA - y.ItemB)
ELSE 0
END) AS t(ItemC)