sql - MS Access: How to use calculated field in a sub query? -


i have problem following query

select a.pname, (b.hours+c.hours) totalhours,    (select top 1 grade tabled tabled.hoursrequire >=**totalhours**)   tablea   left join tableb b on a.pname=b.pname   left join tablec c on a.pname=c.pname 

my problem calculated field "totalhours" not recognized in sub query, appreciated.

will want using field expression (b.hours+c.hours) in subquery instead of alias totalhours?

select a.pname, (b.hours+c.hours) totalhours, (select top 1 grade tabled tabled.hoursrequire >= (b.hours+c.hours)) tablea left join tableb b on a.pname=b.pname left join tablec c on a.pname=c.pname 

however you're using left joins, , suspect rows either b.hours or c.hours null, nothing subquery because b.hours+c.hours null , tabled.hoursrequire >= null never evaluate true. nz() function useful here.

edit: @cyberwiki spot on re top 1 without order by. sorry missed that.

why have hours split between 2 tables (tableb , tablec)? show sample data tables.

edit2: if it's impractical consolidate hours single physical table, can still "virtual" table using union query.

save qryunionhours:

select pname, hours tableb union select pname, hours tablec; 

then can use qryunionhours data source group query, qryhoursperperson, totals hours each person.

select pname, sum(hours) totalhours qryunionhours group pname; 

actually may prefer single query incorporates sql qryunionhours subquery. did 2 because thought might easier understand , less prone syntax errors. need integrate grade assignments discussed in other thread.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -