1
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

4

2 回答 2

3

您将需要使用子查询来访问选择列表中的别名。Select list bill, Net 中使用的别名在 WHERE 子句中不可访问。

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

于 2013-05-21T10:23:16.393 回答
2

正确的语法something != NULLsomething IS NOT NULL

于 2013-05-21T10:30:29.690 回答