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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:14:23+00:00 2026-06-13T23:14:23+00:00

I am trying to get some data from a table and possibly filtering it

  • 0

I am trying to get some data from a table and possibly filtering it depending on the result from another query.

The code is something like this:

DECLARE @TestTable  TABLE(TestID INT)

INSERT INTO @TestTable
SELECT TestID from v_Lookuptable Where SomeID = @input


SELECT DISTINCT
C.[Level],
C.Name
FROM
dbo.CPackage CP 
Right outer join @TestTable TT on TT.TestID = CP.TestID
WHERE
AND (TT.TestID is null OR Exists( Select TT.TestID from @TestTable 
                                    inner join dbo.CProduct CP on CP.TestID = MT.TestID
                                    where TT.TestID = CP.TestID
                                    ))

My problem is that if I pass in my currently scenario the @input as 1, then the top part returns some numbers: 1, 2, 3

But if I pass @input as 2, then the top part returns nothing because nothing matches.

The idea is that if nothing matches, return everything in the bottom query, so from dbo.CPackage, else, only return those things where the TestID match.

I have tried various things like changing the joins, case statements, using ISNULL and collasce. Also I have tried to remove the part TT.TestID is null and it then works if the top table returns something, but when I add it again it returns everything and never filters. So I know the logic is wrong… I just can’t figure out the correct logic for this…. :S

Is it possible? I presume it is…

Cheers
Robin

** Solution **

Remove the join and just put it in the where filter…

SELECT DISTINCT
C.[Level],
C.Name
FROM
dbo.CPackage CP 

WHERE
CP.TestID = coalesce( (select distinct TestID from v_Lookuptable LT
        where LT.SomeID =  @input), CP.TestID )
  • 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-13T23:14:24+00:00Added an answer on June 13, 2026 at 11:14 pm

    Does this give you what you’re looking for:

    SELECT DISTINCT
           CP.[Level]
          ,CP.Name
      FROM dbo.CPackage CP
      LEFT OUTER JOIN @TestTable TT
              ON TT.TestID = CP.TestID
     WHERE CP.TestID = ISNULL(TT.TestID, CP.TestID);
    

    Or, you may even be able to rewrite the entire query like this:

    DECLARE @input1 int;
    SET @input1 = ...;
    
    -- If there are no records returned for v_Lookuptable, then return all records from CPackage.
    -- Otherwise, only return records from CPackage that have a match in the v_Lookuptable where v_Lookuptable.SomeID = @input1.
    SELECT DISTINCT
           CP.[Level]
          ,CP.Name
      FROM dbo.CPackage CP
      LEFT OUTER JOIN v_Lookuptable TT
              ON TT.TestID = CP.TestID
             AND SomeID = @input1 --<== Only include records from TT that match @input1...
     WHERE CP.TestID = ISNULL(TT.TestID, CP.TestID); --<== Return all records from CP if TT is NULL.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code like this trying to get some data from a Documents
I'm trying to get some data from a table, where the tag has no
I am trying to get some form data from POST method. Here's the code
I'm trying to get some data from a certain web-based api using the libcurl
Using php I am trying to get some data from a .csv file which
I have sugar crm instance and i was trying to get some data from
Hi i am trying to get distinct values and some other data from same
I'm trying to get some data from a remote Oracle Database, so I configured
I'm trying to get some data out my templates to, let's say, add an
i am trying to get some data, but i dont know how can i

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.