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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:21:58+00:00 2026-05-11T05:21:58+00:00

I am a noob when it comes to SQL syntax. I have a table

  • 0

I am a noob when it comes to SQL syntax.

I have a table with lots of rows and columns of course 😛 Lets say it looks like this:

      AAA BBB CCC DDD ----------------------- Row1 | 1   A   D   X Row2 | 2   B   C   X Row3 | 3   C   D   Z 

Now I want to create an advanced select statement that gives me this combined (pseudo SQLish here):

select 'Test1', * from TABLE Where CCC='D' AND DDD='X' select 'Test2', * from TABLE Where CCC<>'D' AND DDD='X' 

The output would be:

Test1, 1, A, D, X Test2, 2, B, C, X 

How would I combine those two select statements into one nice select statement?

Would it work if I complicated the SQL like below (because my own SQL statement contains an exists statement)? I just want to know how I can combine the selects and then try to apply it to my somewhat more advanced SQL.

select 'Test1', * from TABLE Where CCC='D' AND DDD='X' AND exists(select ...) select 'Test2', * from TABLE Where CCC<>'D' AND DDD='X' AND exists(select ...) 

My REAL SQL statement is this one:

select Status, * from WorkItems t1 where  exists (select 1 from workitems t2 where t1.TextField01=t2.TextField01 AND (BoolField05=1) ) AND TimeStamp=(select max(t2.TimeStamp) from workitems t2 where t2.TextField01=t1.TextField01)  AND TimeStamp>'2009-02-12 18:00:00' 

which gives me a result. But I want to combine it with a copy of this select statement with an added AND on the end and the ‘Status’ field would be changed with a string like ‘DELETED’.

select 'DELETED', * from WorkItems t1 where  exists (select 1 from workitems t2 where t1.TextField01=t2.TextField01 AND (BoolField05=1) ) AND TimeStamp=(select max(t2.TimeStamp) from workitems t2 where t2.TextField01=t1.TextField01)  AND TimeStamp>'2009-02-12 18:00:00' AND NOT (BoolField05=1) 
  • 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. 2026-05-11T05:21:59+00:00Added an answer on May 11, 2026 at 5:21 am

    You have two choices here. The first is to have two result sets which will set ‘Test1’ or ‘Test2’ based on the condition in the WHERE clause, and then UNION them together:

    select      'Test1', *  from      TABLE  Where      CCC='D' AND DDD='X' AND exists(select ...) UNION select      'Test2', *  from      TABLE Where     CCC<>'D' AND DDD='X' AND exists(select ...) 

    This might be an issue, because you are going to effectively scan/seek on TABLE twice.

    The other solution would be to select from the table once, and set ‘Test1’ or ‘Test2’ based on the conditions in TABLE:

    select      case          when CCC='D' AND DDD='X' AND exists(select ...) then 'Test1'         when CCC<>'D' AND DDD='X' AND exists(select ...) then 'Test2'     end,     *  from      TABLE  Where      (CCC='D' AND DDD='X' AND exists(select ...)) or     (CCC<>'D' AND DDD='X' AND exists(select ...)) 

    The catch here being that you will have to duplicate the filter conditions in the CASE statement and the WHERE statement.

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

Sidebar

Ask A Question

Stats

  • Questions 137k
  • Answers 137k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In your option 1 you are returning from the callback… May 12, 2026 at 7:23 am
  • Editorial Team
    Editorial Team added an answer Once it's redirected to index.php, that file can look at… May 12, 2026 at 7:23 am
  • Editorial Team
    Editorial Team added an answer I use WebSVN with Cruise Control .Net integration, so every… May 12, 2026 at 7:23 am

Related Questions

I am a noob when it comes to python. I have a python script
I am a real noob when it comes to javascript/ajax, so any help will
I'm a total noob when it comes to jQuery but I am trying to
(Didn't mean to create a new question, but revised the old one enough that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.