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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:40:59+00:00 2026-06-13T19:40:59+00:00

is it possible to have if statements inside an outer apply? I can’t seem

  • 0

is it possible to have if statements inside an outer apply? I can’t seem to get it work.

this is the existing code that works:

select id
from
residentmemberlease rml
left outer join lease l on rml.leaid = l.leaid
outer apply
(SELECT TOP(1) * FROM dbo.RentalHistory a WITH(NOLOCK) WHERE a.resmID = RML.resmID and rthDisplayBit='1'
ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC) AS RH

i want it to look like this depending on the value of the codeleasestatuscode(if its 1 or 2):

select id
from
residentmemberlease rml
left outer join lease l on rml.leaid = l.leaid
outer apply
if(select codeleasestatuscode from lease) = '1'
begin
(SELECT TOP(1) * FROM dbo.RentalHistory a WITH(NOLOCK) WHERE a.resmID = RML.resmID and rthDisplayBit='1'
ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC) AS RH
end
if(select codeleasestatuscode from lease) = '2'
begin
(SELECT TOP(1) * FROM dbo.RentalHistory a WITH(NOLOCK) WHERE a.resmID = RML.resmID and rthDisplayBit='0'
ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC) AS RH
end
  • 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-13T19:41:01+00:00Added an answer on June 13, 2026 at 7:41 pm
           SELECT id --<<-- best to add aliases to all columns
             FROM residentmemberlease rml
        LEFT JOIN lease l ON rml.leaid = l.leaid
      OUTER APPLY
                (SELECT TOP(1) *
                   FROM dbo.RentalHistory a WITH(NOLOCK)
                  WHERE a.resmID = RML.resmID
                    AND (l.codeleasestatuscode = '1' AND a.rthDisplayBit='1' OR
                         l.codeleasestatuscode = '2' AND a.rthDisplayBit='0')
               ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC
                ) AS RH;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some sql statements that calculates some numbers. It's possible (with bonus points/penalty
Not sure if it's possible, but can you use if statements inside a template?
I have a java code with pig statements embedded inside using the registerQuery() method
Suppose that we have this code (simplified example): $propertyName = 'items'; $foo = new
Possible Duplicate: Using a bitwise & inside an if statement I have the following
Is this possible at all? The idea is to have the following: if ($some_statement)
Is it possible to have a free iPhone app that has say an initial
I have a vb.net application which does some processing. This processing can take a
In getFieldSignExtended(int,int,int) , I have if-else statements inside if-else statements. I have int result
Possible Duplicate: Format Date in Bind Statement inside a ListView I have modified a

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.