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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:14:20+00:00 2026-06-04T22:14:20+00:00

I need help to write this in SQL: I have one table and I

  • 0

I need help to write this in SQL:

I have one table and I would like to select No with Chars A, B or C, but if I have A and B for same No I want to show only B, else I need to show all.

Pseudo code:

SELECT *
  FROM Table1
  WHERE IF EXISTS (Char = A and Char = B)
    THEN Char IN (B, C)
    ELSE Char IN (A, B, C)

Example:

Table1

ID  No     Char
1   10     A
2   10     B
4   10     C
5   11     A
6   11     D
7   12     C

I want result:

ID  No     Char
2   10     B
4   10     C
5   11     A
7   12     C

Is it clear enough what I need?

  • 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-04T22:14:22+00:00Added an answer on June 4, 2026 at 10:14 pm
    select t1.Id, t1.No, t1.Ch from Table1 t1
    where t1.Ch IN ( 'B', 'C')
    UNION
    select t1.Id, t1.No, t1.Ch from Table1 t1
    where t1.Ch ='A' 
    and not exists (select null from Table1 t2 where t2.No = t1.No  and t2.Ch='B')
    

    I changed Char in Ch as Char might be a reserved KeyWord…

    Tested with Sql Server, but the query is, I think, ANSI SQL

    http://sqlfiddle.com/#!3/f2145/12

    or

    select t1.Id, t1.No, t1.Ch from Table1 t1
    where t1.Ch IN ( 'B', 'C')
    OR (t1.Ch ='A'
    and not exists (select null from Table1 t2 where t2.No = t1.No  and t2.Ch='B'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help/guide for sql select query, I have 2 table stock and stock_history, in
need help in error in database pivot. i have table tamed table_score like below:
I need to write an sql with the below requirements.. I have a table
I have a SQL table called StudentMarks.which consist of StudentID,SubjectName,SubjectMark I want to write
I have a InsuranceDetails SQL table, one of the column is VIN whose type
I have a table INFO whose design is like this id - bigint Name
I have a table like this: Table name: PRODUCT ID PRODUCTID PRODUCTNAME 1 D100
This is a really conviluted piece of SQL, I know, but basically one of
Need help from you all in writing up this query. Running SQL 2005 Standard
I need a little help on this subject. I have a Web application written

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.