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

  • SEARCH
  • Home
  • 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 9127545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:18:23+00:00 2026-06-17T07:18:23+00:00

I want to join two tables in a query extracting some information from each,

  • 0

I want to join two tables in a query extracting some information from each, but the second table requires a TOP 1:

This gets my list of people

SELECT [Clock no], [Card id], [Current pay cat], [Pay category] 
FROM Employees EMP 
WHERE [Clocked in flag] = 'Y'

Now for each of the people in that table I want to join to this query:

SELECT TOP 1 [Start time], Dated FROM [Work records] WR 
WHERE WR.[Clock no] = XXXXXXXXX <- Clock no from first query
AND WR.Dated <= '2013-01-07' AND WR.[Open flag] = 'Y' 
ORDER BY WR.[Dated] DESC, WR.[Start time] DESC

What I have so far is something like this, but I can’t seem to get it to work as soon as I add the ORDER By clause:

SELECT EMP.[Clock no], [Card id], [Current pay cat], [Pay category], WRTOP.[Start time], WRTOP.[Dated]
FROM Employees EMP 
LEFT JOIN (
SELECT TOP 1 [Clock no], [Start time], Dated FROM [Work records] WR 
WHERE WR.Dated <= '2013-01-07' AND WR.[Open flag] = 'Y' 
ORDER BY WR.[Dated] DESC, WR.[Start time] DESC
) WRTOP 
ON (EMP.[Clock no] = WRTOP.[Clock no])
WHERE EMP.[Clocked in flag] = 'Y'
  • 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-06-17T07:18:24+00:00Added an answer on June 17, 2026 at 7:18 am

    The subquery returns one row, not necessarily for the right Clock no. Only looking at the right Clock no requires a relation between the subquery and an outer table. That’s not allowed in join.

    One way around that is a subquery in the on condition, like:

    SELECT  EMP.[Clock no]
    ,       EMP.[Card id] 
    ,       EMP.[Current pay cat]
    ,       EMP.[Pay category]
    ,       WR.[Start time]
    ,       WR.[Dated]
    FROM    Employees EMP 
    JOIN    [Work records] WR 
    ON      WR.[Open flag] = 'Y' 
            AND WR.[Clock no] = EMP.[Clock no]
            AND WR.Dated =
            (
            SELECT  max(Dated)
            FROM    [Work records] WR2
            WHERE   WR2.[Open flag] = 'Y'
                    AND WR2.[Clock no] = EMP.[Clock no]
                    AND WR2.Dated <= '2013-01-07'
            )
            AND WR.[Start time] =
            (
            SELECT  max([Start time])
            FROM    [Work records] WR3
            WHERE   WR3.[Open flag] = 'Y'
                    AND WR3.[Clock no] = EMP.[Clock no]
                    AND WR3.Dated = 
                    (
                    SELECT  max(Dated)
                    FROM    [Work records] WR4
                    WHERE   WR4.[Open flag] = 'Y'
                            AND WR4.[Clock no] = EMP.[Clock no]
                            AND WR4.Dated <= '2013-01-07'
                    )
            )
    WHERE EMP.[Clocked in flag] = 'Y'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've to query from two tables and want one result.. how can i join
I want to left join two tables. My problem is that the table from
I want to join two tables using JPQL : SELECT * FROM A LEFT
I want to join two tables (below) which i get. But then I want
Hi i want to display join result of two tables but don't want to
I want to join two temp tables. Here are my tables: CREATE TABLE #Result
I want to FULL JOIN following two tables Table 1: Persons P_Id Name 1
I want to display a FULL JOIN of two tables, but using an intermediary
In my query i want to join two tables based on the value of
I have a SQL query where I want to join two tables to gether

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.