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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:54:15+00:00 2026-05-24T17:54:15+00:00

I need a temporary solution to a problem I’ve created. Essentially I want to

  • 0

I need a temporary solution to a problem I’ve created. Essentially I want to calculate two values, but use a different method dependant on the result of a condition.

select userReturnval, userregisterid, OtherValue
FROM
(
   (SELECT otherValue
   FROM...
   ) as tblO  --unrelated table
  ,

  ( 
    if (select count(userregisterid) from table1 where site =@siteID and userid=@userID) >0
      SELECT userReturnval, userregisterid
    FROM
    (
     SELECT userReturnval, userregisterid, Rank() OVER (PARTITION BY .. ORDER BY       ...) as RANK
            FROM ...
            WHERE --first where clause
        ) as tblRank
        WHERE (RANK =1)
    else
      SELECT userReturnval, userregisterid
      FROM
        (
     SELECT userReturnval, userregisterid, Rank() OVER (PARTITION BY .. ORDER BY       ...) as RANK
            FROM ...
            WHERE --different where clause
        ) as tblRank
        WHERE (RANK =1)

  ) as tblR

My if works fine on its own, I just to get it working as part of the larger query. At the moment, sqlserver doesn’t like the if being in there.

Hopefully someone can point me in the right direction!

  • 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-24T17:54:16+00:00Added an answer on May 24, 2026 at 5:54 pm

    You can try using case statements in the WHERE clause, something like the statement below. Note that I don’t think this will be particularly optimal for performance.

    Doing it like this does allow you to keep it to a single statement though:

    select userReturnval, userregisterid, OtherValue
    FROM
    (
       (SELECT otherValue
       FROM...
       ) as tblO  --unrelated table
      ,
    
      ( 
        SELECT userReturnval, userregisterid
        FROM
        (
         SELECT userReturnval, userregisterid, Rank() OVER (PARTITION BY .. ORDER BY       ...) as RANK
                FROM ...
                WHERE 
                    case --Choose which where clause to use
                        when (select count(userregisterid) from table1 where site =@siteID and userid=@userID) >0 then 
                            case when /*First where clause*/ then 1
                            else 0
                            end
                        else
                            case when /*Second where clause*/ then 1
                            else 0
                            end
                        end
                     = 1                
            ) as tblRank
            WHERE (RANK =1)
      ) as tblR
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to setup temporary User models for each visitors, where the visitors are
I need a fresh temporary directory to do some work in a shell script.
I need to delete a temporary file from my C++ windows application (developed in
I need to write a temporary Web-based graphical front-end for a custom server system.
I'd need a solution like AppFabric to store large files (up to 500 MB)
I've a recurrent problem, I don't find an elegant solution to avoid the resource
Thanks to this answer I managed to come up with a temporary solution to
I've done a bit of googling round but this particular problem is a little
I am hoping to use the PayPal Pro Hosted Solution to handle payments for
I have this problem. I create two instaces of my classes in main window.

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.