Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3797632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:26:03+00:00 2026-05-19T13:26:03+00:00

I have problem with the following query Select A.PName, (B.Hours+C.Hours) as TotalHours, (select top

  • 0

I have problem with the following query

Select A.PName, (B.Hours+C.Hours) as TotalHours,   
(select top 1 grade from TableD where TableD.HoursRequire >=**TotalHours**)  
from TableA A  
left join TableB B on A.Pname=B.Pname  
left join TableC C on A.Pname=C.Pname

My problem is that the calculated field “TotalHours” is not recognized in the sub query, any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:26:04+00:00Added an answer on May 19, 2026 at 1:26 pm

    Will you get what you want by using the field expression (B.Hours+C.Hours) in the subquery instead of the alias TotalHours?

    Select A.PName, (B.Hours+C.Hours) as TotalHours,
    (select top 1 grade from TableD where TableD.HoursRequire >= (B.Hours+C.Hours))
    from TableA A
    left join TableB B on A.Pname=B.Pname
    left join TableC C on A.Pname=C.Pname
    

    However you’re using left joins, and I suspect for rows where either B.Hours or C.Hours is Null, you will get nothing from the subquery because B.Hours+C.Hours will be Null and TableD.HoursRequire >= Null will never evaluate as True. The Nz() function could be useful here.

    Edit: @cyberwiki is spot on Re Top 1 without ORDER BY. Sorry I missed that.

    Why do you have Hours split between 2 tables (TableB and TableC)? Show us some sample data from those tables.

    Edit2: If it’s impractical to consolidate Hours into a single physical table, you can still do it with a “virtual” table using a Union query.

    Save this as qryUnionHours:

    SELECT Pname, Hours
    FROM TableB
    UNION ALL
    SELECT Pname, Hours
    FROM TableC;
    

    Then you can use qryUnionHours as the data source for a GROUP BY query, qryHoursPerPerson, which totals the hours for each person.

    SELECT Pname, Sum(Hours) AS TotalHours
    FROM qryUnionHours
    GROUP BY Pname;
    

    Actually you may prefer a single query which incorporates the SQL from qryUnionHours as a subquery. I did it with 2 because I thought it might be easier to understand and less prone to syntax errors. All you need to do then is integrate your Grade assignments as we discussed in your other thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT products_categories.categoryID, name, COUNT(*) AS itemCount FROM products_categories LEFT
I have a problem with the following HQL query: select sum(MYTABLE.COUNTER) from ( select
I have a problem with the following MySQL query: SELECT * FROM TPDSessions WHERE
I have a problem with the following Linq query using Entity Framework: from o
I have the following query: SELECT * from foo where timestamp = (select max(timestamp)
I have the following query: SELECT m.*, COUNT(c.chapter_nr) as chapters, MAX(c.chapter_nr) as latest_chapter FROM
I have the following query: SELECT dev.DeviceName, Count(dom.DomainID) AS CountOfDomains FROM tblDevices dev JOIN
I have the following Query: SELECT a.name_f, a.uid, a.name_l, a.profile_pic, b.position FROM users a
I have the following query: SELECT MIN(myColumn) as myColumn FROM myTable WHERE date(time_stamp) =
I have the following query SELECT * FROM( SELECT CASE WHEN TO_CHAR (ADD_MONTHS(:DATEINPUT, 1),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.