sql server - Invalid column name when where condition while using case in SQL -


select     sdate,    sum(case when cgrp!='township' , cdcode=0 , sdate between '4/1/2013' , '4/1/2013' billamt end) bill,    sum(case  when cgrp!='township' , cdcode!=0 , sdate between '4/1/2013' , '4/1/2013' billamt end) net indent bill != null      group sdate 

the error message points line bill != null

you need use subquery access alias name in select list . alias used in select list bill , net not accessible in clause .

select sdate,bill,net ( select sdate,  sum(case when cgrp!='township' , cdcode=0 , sdate between '4/1/2013' , '4/1/2013' billamt end)as bill,  sum(case when cgrp!='township' , cdcode!=0 , sdate between '4/1/2013' , '4/1/2013' billamt end)as net  indent  group sdate )z z.bill not null 

this because in sequence of query execution , clause executed first , select .

general sequence of query execution.

1 . 2 . 3 . group 4 . select 5 . order  

in query's clause , using alias name bill since alias defined later in select , invalid column error thrown .

also null comparison use is / not null


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -