select
sdate,
SUM(case when CGrp!='TOWNSHIP' and cdcode=0 and SDate between '4/1/2013' and '4/1/2013' then BillAmt end) as bill,
sum(case when CGrp!='TOWNSHIP' and cdcode!=0 and SDate between '4/1/2013' and '4/1/2013' then BillAmt end) as Net
from Indent
where bill != null
group by SDate
错误消息指向bill != null
答案 0 :(得分:3)
您需要使用子查询来访问选择列表中的别名。 在WHERE子句中无法访问Select list bill,Net中使用的别名。
select sdate,bill,Net from
(
select sdate,
SUM(case when CGrp!='TOWNSHIP' and cdcode=0 and SDate between '4/1/2013' and '4/1/2013' then BillAmt end)as bill,
SUM(case when CGrp!='TOWNSHIP' and cdcode!=0 and SDate between '4/1/2013' and '4/1/2013' then BillAmt end)as Net
from Indent group by SDate
)z
WHERE z.bill IS NOT NULL
这是因为在查询执行的顺序中,首先执行WHERE子句,然后执行SELECT。
查询执行的一般顺序。
1 . FROM
2 . WHERE
3 . GROUP BY
4 . SELECT
5 . ORDER BY
在查询的WHERE子句中,您使用的是别名bill
,但由于此别名稍后在SELECT中定义,因此会引发无效的列错误。
对于NULL比较,请使用IS / IS NOT NULL
答案 1 :(得分:2)
something != NULL
的正确语法为something IS NOT NULL