我们如何过滤别名?例如,在下面的查询中,对于别名OLB,如果我们想说WHERE OLB> 0
SELECT
loan.lnr
,loan.lamount - SUM (memrepay.mprinc) AS OLB
,lstatus
FROM
loan
LEFT OUTER JOIN memrepay
ON loan.lnr = memrepay.lnr
WHERE
loan.tstart >= N'2013-07-07T14:00:00'
GROUP BY loan.lnr, lamount, lstatus
Order By lnr
答案 0 :(得分:3)
使用派生表
select t.* from
(
SELECT
loan.lnr
,loan.lamount - SUM (memrepay.mprinc) AS OLB
,lstatus
FROM
loan
LEFT OUTER JOIN memrepay
ON loan.lnr = memrepay.lnr
WHERE
loan.tstart >= N'2013-07-07T14:00:00'
GROUP BY loan.lnr, lamount, lstatus
) as t where OLB>0
Order By lnr
或使用HAVING子句并使用表达式
SELECT
loan.lnr
,loan.lamount - SUM (memrepay.mprinc) AS OLB
,lstatus
FROM
loan
LEFT OUTER JOIN memrepay
ON loan.lnr = memrepay.lnr
WHERE
loan.tstart >= N'2013-07-07T14:00:00'
GROUP BY loan.lnr, lamount, lstatus
HAVING loan.lamount - SUM (memrepay.mprinc) >0
Order By lnr