sql server - Alias not recognised in subquery case statement SQL -
i have following query runs fine when isn't populated i'm having difficulty getting right references data in where.
code below:
select g.id id, g.printed printed, g.createddate createddate , case when ( select count(distinct custsubga.customerid) pickingassignment_pickingassignmentusergroup custsubg join pickingassignment custsubga on custsubga.id = custsubg.pickingassignmentid join customer custsub2c on custsubga.customerid = custsub2c.id custsubg.pickingassignmentusergroupid = max(g.id) ) > 1 'multiple' else ( select max(custsub2c.name) pickingassignment_pickingassignmentusergroup custsub2g join pickingassignment custsub2ga on custsub2g.pickingassignmentid = custsub2ga.id join customer custsub2c on custsub2ga.customerid = custsub2c.id custsub2g.pickingassignmentusergroupid = max(g.id) ) end customername pickingassignmentusergroup g customername 'exet%'
i tried using " custsub2c.name 'exet%' , didn't work either. multi-part identifier custsub2c.name not bound that.
this first time i've attempted case statements in sql there's chance i'm doing very wrong.
any fantastic. please let me know if need more information.
you cannot use column alias in where
statement @ same level. in databases, use subquery:
select * (select g.id id, g.printed printed, g.createddate createddate , case when (select count(distinct custsubga.customerid) pickingassignment_pickingassignmentusergroup custsubg join pickingassignment custsubga on custsubga.id = custsubg.pickingassignmentid join customer custsub2c on custsubga.customerid = custsub2c.id custsubg.pickingassignmentusergroupid = max(g.id) ) > 1 'multiple' else (select max(custsub2c.name) pickingassignment_pickingassignmentusergroup custsub2g join pickingassignment custsub2ga on custsub2g.pickingassignmentid = custsub2ga.id join customer custsub2c on custsub2ga.customerid = custsub2c.id custsub2g.pickingassignmentusergroupid = max(g.id) ) end customername pickingassignmentusergroup g ) t customername 'exet%'
Comments
Post a Comment