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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:11:31+00:00 2026-06-04T18:11:31+00:00

I have a query where I need a correlated subquery inside a join. I

  • 0

I have a query where I need a correlated subquery inside a join. I say need, what I mean is that I think I need it there… How can I do this? My query is below, I get the “The multi-part identifier “FIELDNAME GOES HERE” could not be bound” error… How can I change this query to work?

SELECT FireEvent.ExerciseID, 
       FireEvent.FireEventID, 
       tempHitEvent.HitEventID, 
       FireEvent.AssociatedPlayerID, 
       tempHitEvent.AssociatedPlayerID, 
       FireEvent.EventTime, 
       tempHitEvent.EventTime, 
       FireEvent.Longitude, 
       FireEvent.Latitude, 
       tempHitEvent.Longitude, 
       tempHitEvent.Latitude, 
       tempHitEvent.HitResult, 
       FireEvent.AmmunitionCode, 
       FireEvent.AmmunitionSource, 
       FireEvent.FireEventID, 
       0 AS 'IsArtillery' 
FROM   FireEvent 
       LEFT JOIN (SELECT HitEvent.*, 
                         FireEvent.FireEventID, 
                         Rank() 
                           OVER ( 
                             ORDER BY HitEvent.EventTime) AS RankValue 
                  FROM   HitEvent 
                         WHERE FireEvent.EventTime BETWEEN 
                                    Dateadd(millisecond, -5000, 
                                    HitEvent.EventTime) AND 
                                               Dateadd(millisecond, 
                                               5000, HitEvent.EventTime) AND HitEvent.FiringPlayerID = FireEvent.PlayerID 
                   AND HitEvent.AmmunitionCode = 
                       FireEvent.AmmunitionCode
                   AND HitEvent.ExerciseID = 
                       'D289D508-1479-4C17-988C-5F6A847AE51E' 
                        AND FireEvent.ExerciseID = 
                       'D289D508-1479-4C17-988C-5F6A847AE51E' 
                   AND HitEvent.HitResult NOT IN ( 0, 1 ) ) AS 
                 tempHitEvent 
              ON ( 
              RankValue = 1
            AND tempHitEvent.FireEventID = 
                     FireEvent.FireEventID 
                     )
WHERE  FireEvent.ExerciseID = 'D289D508-1479-4C17-988C-5F6A847AE51E' 
ORDER BY HitEventID
  • 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-04T18:11:32+00:00Added an answer on June 4, 2026 at 6:11 pm

    Use OUTER APPLY instead of LEFT JOIN. I have had to move some of your clauses around, but the below should produce the desired result

    SELECT  FireEvent.ExerciseID, 
            FireEvent.FireEventID, 
            tempHitEvent.HitEventID, 
            FireEvent.AssociatedPlayerID, 
            tempHitEvent.AssociatedPlayerID, 
            FireEvent.EventTime, 
            tempHitEvent.EventTime, 
            FireEvent.Longitude, 
            FireEvent.Latitude, 
            tempHitEvent.Longitude, 
            tempHitEvent.Latitude, 
            tempHitEvent.HitResult, 
            FireEvent.AmmunitionCode, 
            FireEvent.AmmunitionSource, 
            FireEvent.FireEventID, 
            0 AS 'IsArtillery' 
    FROM    FireEvent 
            OUTER APPLY
            (   SELECT  HitEvent.*, RANK() OVER (ORDER BY HitEvent.EventTime) AS RankValue 
                FROM    HitEvent 
                WHERE   HitEvent.FireEventID = FireEvent.FireEventID 
                AND     FireEvent.EventTime BETWEEN DATEADD(MILLISECOND, -5000, HitEvent.EventTime) AND DATEADD(MILLISECOND, 5000, HitEvent.EventTime) 
                AND     HitEvent.FiringPlayerID = FireEvent.PlayerID 
                AND     HitEvent.AmmunitionCode = FireEvent.AmmunitionCode
                AND     HitEvent.ExerciseID = FireEvent.ExerciseID
                AND     HitEvent.HitResult NOT IN ( 0, 1 ) 
            ) AS tempHitEvent 
    WHERE   COALESCE(RankValue, 1) = 1
    AND     FireEvent.ExerciseID = 'D289D508-1479-4C17-988C-5F6A847AE51E' 
    ORDER BY FireEvent.HitEventID
    

    If you only want to return results where there is a matching HitEvent use CROSS APPLY. CROSS APPLY is to OUTER APPLY what INNER JOIN is to LEFT JOIN.


    ADDENDUM

    This can all be achieved using joins with no need for OUTER APPLY, by moving not using a subquery to join HitEvent, then performing the RANK function on all data, not just the HitEvent table. This all needs to be moved to a subquery so the result of the RANK function can be inlcuded in a WHERE clause.

    SELECT  *
    FROM    (   SELECT  FireEvent.ExerciseID, 
                        FireEvent.FireEventID, 
                        HitEvent.HitEventID, 
                        FireEvent.AssociatedPlayerID, 
                        --HitEvent.AssociatedPlayerID, 
                        FireEvent.EventTime, 
                        HitEvent.EventTime [HitEventTime], 
                        FireEvent.Longitude [FireEventLongitute], 
                        FireEvent.Latitude [FireEventLatitute], 
                        HitEvent.Longitude [HitEventLongitute], 
                        HitEvent.Latitude [HitEventLatitute], 
                        HitEvent.HitResult , 
                        FireEvent.AmmunitionCode, 
                        FireEvent.AmmunitionSource,
                        0 [IsArtillery],
                        RANK() OVER(PARTITION BY HitEvent.FireEventID, HitEvent.FiringPlayerID, HitEvent.AmmunitionCode,HitEvent.ExerciseID ORDER BY HitEvent.EventTime) [RankValue]
                FROM    FireEvent 
                        LEFT JOIN HitEvent
                            ON HitEvent.FireEventID = FireEvent.FireEventID 
                            AND FireEvent.EventTime BETWEEN DATEADD(MILLISECOND, -5000, HitEvent.EventTime) AND DATEADD(MILLISECOND, 5000, HitEvent.EventTime) 
                            AND HitEvent.FiringPlayerID = FireEvent.PlayerID 
                            AND HitEvent.AmmunitionCode = FireEvent.AmmunitionCode
                            AND HitEvent.ExerciseID = FireEvent.ExerciseID
                            AND HitEvent.HitResult NOT IN ( 0, 1 ) 
            ) data
    WHERE   RanKValue = 1
    

    This may offer slightly improved performance compared to using OUTER APPLY, it may not. It depends on your schema and the amount of data you are processing. There is no substitute for testing:

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

Sidebar

Related Questions

Say I have a query that fetches [type][show_name]. For all [type]==5 records, I need
I have a query that I need to execute that I do not know
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a query such that I need to get A specific dog All
I have a query that does what i want joining table but i need
Using Microsoft SQL server 2008 I have a query that is in need of
Say I have 3 queries. Query 1 returns a piece of information that Query
I have query that gives back results like this: Client1 Product1 $500 Client1 Product2
I have a query where I need to select text with a colon inside,
I have a query where I need to get post + every tag that

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.