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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:17:27+00:00 2026-05-28T20:17:27+00:00

say I have the following simple table table a { id int, pointId int,

  • 0

say I have the following simple table

table a
{
id int,
pointId int,
state int,
value int
}

id    pointId     state   value
-------------------------------
1     1           2       xxxxx
2     3           2       xxxxx
3     3           2       xxxxx
4     3           1       xxxxx

with the following two queries:

SELECT * FROM a WHERE state = 1 and pointId = x
SELECT * FROM a WHERE state = 2 and pointId = x

Is there a way to combine the two queries so that:

  • if query 1 returns >0 rows, it returns the result from query 1
  • if query 1 returns 0 rows, it returns the result from query 2

(so for pointId = 1 it would return row 1, for pointId = 3 it would return row 4)

I’ve been trying to combine the two with a union doing something like

SELECT * FROM a WHERE state = 1 AND pointId = x
UNION ALL
SELECT * FROM a WHERE state = 2 AND pointId = x and 
pointId NOT IN (SELECT pointId FROM a WHERE state = 1 AND pointId = x)

I’m wondering if I’m making this way to complicated and if there’s a more easy way. I realize I could just run the first query and then handle it in my code, but it would be really nice if I could do this in the sql.
Any advice or pointers would be great!

Thanks,

A

  • 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-28T20:17:28+00:00Added an answer on May 28, 2026 at 8:17 pm

    UNION is used to combine tuples, your specification mentions you want to return 1 set of tuples or another set of tuples. In this case you don’t want to use UNION, you can use EXISTS:

    IF EXISTS(...insert your first query here...)
        BEGIN
           --a record exists process first query
        END
    ELSE
        BEGIN
             --do other query
        END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following simple table variable: declare @databases table ( DatabaseID
Say I have a simple table, named content with the following fields: id ,
Let's say I have the following table in SQL Server 2008: ProfileID int //identity;
Say I have the following simple java bean: class MyBean { private Date startDate;
Let's say I have the following simple enum: enum Response { Yes = 1,
Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
Say I have the following table, used to get a list of delta updates
Let's say we have the following mega-simple Python script: print Initializing.... a=10 print Variable
Let's say I have the following MySQL table: id | species ------------ 1 |
Ok, say I have a query: SELECT * FROM TABLE_AWESOME WHERE YEAR = :AMAZINGYEAR;

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.