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 8929727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:44:11+00:00 2026-06-15T08:44:11+00:00

I am trying to join one record in a table to another using a

  • 0

I am trying to join one record in a table to another using a derived table and am having a bit of trouble figuring out the correct query to do so. What I want to do is have a JOIN of a derived table to a query where the derived table uses where statements depending on data from the outer query that is being joined to. So here is the current code that I am working on:

SELECT a.viewerid, a.id, v.id AS entry, a.jobid, v.sourceid, v.cost, a.applicant
FROM a_views a,
JOIN (
    SELECT TOP 1 id, sourceid, cost FROM a_views vt
    WHERE vt.viewerid = a.viewerid
      AND vt.viewed_at <= a.viewed_at
      AND vt.referrer NOT LIKE '%' + vt.hostName + '%'
      ORDER BY viewed_at DESC
) v

The derived table is a query of the same table that the outer query uses, and viewerid is a FK to itself across the table where id is a unique auto-incrementing PK. I need to get the latest record in the a_views table where the viewer id’s match, the datestamp (viewed_at) is less than the outer datestamp and the referrer column doesn’t contain the hostName column.

  • 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-15T08:44:12+00:00Added an answer on June 15, 2026 at 8:44 am

    Sounds like you need APPLY:

    SELECT a.viewerid, a.id, v.id AS entry, a.jobid, v.sourceid, v.cost, a.applicant
    FROM a_views a
        CROSS APPLY (
            SELECT TOP 1 id, sourceid, cost FROM a_views vt
            WHERE vt.viewerid = a.viewerid
              AND vt.viewed_at <= a.viewed_at
              AND vt.referrer NOT LIKE '%' + vt.hostName + '%'
              ORDER BY viewed_at DESC
        ) v
    

    Since your query has JOIN I’ve gone for CROSS APPLY, but you may need OUTER APPLY depending on your exact requirements.

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

Sidebar

Related Questions

I am trying to join my users table with another table using the following
I'm trying to join 2 sql queries in one query. The first one gets
I am trying to create a delete query to remove records from one table,
I'm trying to find one record in the Meeting table that contains the same
I'm trying to join a query results on a field, 'min' - which is
I am trying to join 3 table in LINQ. var fromCities = from c
I'm trying to update a DATETIME field in one table based on the latest
I'm trying to figure out how best to query against a schema that consists
I am trying to join two tables: table A and table B. Table A
I am trying to join 2 tables, but only join with the latest record

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.