请您查看代码并注意我不能在“联盟”的任何一侧的主要Select Statement Query中使用“MYDELTOT”
代码:
select
'POID' = DYN_PORDERS.ID, 'PSID' = SYS_SUPPLIERS.ID,
(select sum(DYN_PORDERDELS.DelQty) as MMTD
from DYN_PORDERDELS
where DYN_PORDERDELS.DelPOID = DYN_PORDERS.ID) as MYDELTOT,
*
from
DYN_PORDERSRS
inner join
DYN_PORDERS on DYN_PORDERS.id = DYN_PORDERSRS.RSOrderID
inner join
SYS_SUPPLIERS on SYS_SUPPLIERS.id = DYN_PORDERS.SupplierID
inner join
DYN_porderdels on DYN_PORDERS.ID = DYN_PORDERDELS.DelPOID
where
DYN_PORDERSRS.RSDate <= '20151031'
and DYN_PORDERS.Qnty >= MYDELTOT
union
select
'POID' = DYN_PORDERS.ID, 'PSID' = SYS_SUPPLIERS.ID,
(select sum(DYN_PORDERDELS.DelQty) as MMTD
from DYN_PORDERDELS
where DYN_PORDERDELS.DelPOID = DYN_PORDERS.ID) as MYDELTOT,
*
from
DYN_PORDERSRS
inner join
DYN_PORDERS on DYN_PORDERS.id = DYN_PORDERSRS.RSOrderID
inner join
SYS_SUPPLIERS on SYS_SUPPLIERS.id = DYN_PORDERS.SupplierID
inner join
DYN_porderdels on DYN_PORDERS.ID = DYN_PORDERDELS.DelPOID
where
DYN_PORDERSRS.rsdate >= '20151101'
and DYN_PORDERS.Qnty >= MYDELTOT
请帮忙
由于 麦克
答案 0 :(得分:2)
您可以尝试在以下内容中使用CTE
:
;with cte as (
select 'POID' = DYN_PORDERS.ID, 'PSID' = SYS_SUPPLIERS.ID,
(select sum(DYN_PORDERDELS.DelQty) as MMTD from DYN_PORDERDELS where
DYN_PORDERDELS.DelPOID = DYN_PORDERS.ID) as MYDELTOT,
* from DYN_PORDERSRS
inner join DYN_PORDERS on DYN_PORDERS.id=DYN_PORDERSRS.RSOrderID
inner join SYS_SUPPLIERS on SYS_SUPPLIERS.id=DYN_PORDERS.SupplierID
inner join DYN_porderdels on DYN_PORDERS.ID = DYN_PORDERDELS.DelPOID
where DYN_PORDERSRS.RSDate <= '20151031'
union
select 'POID' = DYN_PORDERS.ID, 'PSID' = SYS_SUPPLIERS.ID,
(select sum(DYN_PORDERDELS.DelQty) as MMTD from DYN_PORDERDELS where
DYN_PORDERDELS.DelPOID = DYN_PORDERS.ID) as MYDELTOT,
* from DYN_PORDERSRS
inner join DYN_PORDERS on DYN_PORDERS.id=DYN_PORDERSRS.RSOrderID
inner join SYS_SUPPLIERS on SYS_SUPPLIERS.id=DYN_PORDERS.SupplierID
inner join DYN_porderdels on DYN_PORDERS.ID = DYN_PORDERDELS.DelPOID
where DYN_PORDERSRS.rsdate >= '20151101'
)
select *
from cte
where Qnty >= MYDELTOT
答案 1 :(得分:1)
你的sql看起来有点滑稽但是应该这样做.....今天第二个
SELECT q.* FROM
(
select 'POID' = DYN_PORDERS.ID, 'PSID' = SYS_SUPPLIERS.ID, DYN_PORDERS.Qnty,
(select sum(DYN_PORDERDELS.DelQty) as MMTD from DYN_PORDERDELS where
DYN_PORDERDELS.DelPOID = DYN_PORDERS.ID) as MYDELTOT,
* from DYN_PORDERSRS
inner join DYN_PORDERS on DYN_PORDERS.id=DYN_PORDERSRS.RSOrderID
inner join SYS_SUPPLIERS on SYS_SUPPLIERS.id=DYN_PORDERS.SupplierID
inner join DYN_porderdels on DYN_PORDERS.ID = DYN_PORDERDELS.DelPOID
where DYN_PORDERSRS.RSDate <= '20151031'
union
select 'POID' = DYN_PORDERS.ID, 'PSID' = SYS_SUPPLIERS.ID, DYN_PORDERS.Qnty,
(select sum(DYN_PORDERDELS.DelQty) as MMTD from DYN_PORDERDELS where
DYN_PORDERDELS.DelPOID = DYN_PORDERS.ID) as MYDELTOT,
* from DYN_PORDERSRS
inner join DYN_PORDERS on DYN_PORDERS.id=DYN_PORDERSRS.RSOrderID
inner join SYS_SUPPLIERS on SYS_SUPPLIERS.id=DYN_PORDERS.SupplierID
inner join DYN_porderdels on DYN_PORDERS.ID = DYN_PORDERDELS.DelPOID
where DYN_PORDERSRS.rsdate >= '20151101'
) q
where
AND q.Qnty >= q.MYDELTOT
答案 2 :(得分:0)
在WHERE
条件之后应用别名,这意味着在应用条件时没有任何名称为MYDELTOT
。
解决这个问题的一种方法是在子查询中创建别名,别名将与子查询级别的值相关联
select 'POID' = DYN_PORDERS.ID
, 'PSID' = SYS_SUPPLIERS.ID
, t.MYDELTOT
, *
from DYN_PORDERSRS
inner join DYN_PORDERS on DYN_PORDERS.id = DYN_PORDERSRS.RSOrderID
inner join SYS_SUPPLIERS on SYS_SUPPLIERS.id = DYN_PORDERS.SupplierID
inner join DYN_porderdels on DYN_PORDERS.ID = DYN_PORDERDELS.DelPOID
CROSS APPLY (select sum(DYN_PORDERDELS.DelQty) as MYDELTOT
from DYN_PORDERDELS
where DYN_PORDERDELS.DelPOID = DYN_PORDERS.ID
) as t
where DYN_PORDERSRS.rsdate <= '20151031'
and DYN_PORDERS.Qnty >= t.MYDELTOT