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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:17:25+00:00 2026-06-17T12:17:25+00:00

I am new to SQL, so I’m not sure which approach is best for

  • 0

I am new to SQL, so I’m not sure which approach is best for this kind of task:

I have a table where groups of rows all relate to the same item, whose name appears in the first column. Other details appear in the other columns. I am trying to retrieve all rows for every group based on having the same value in the first column, where every time a certain value appears in one column, another value appears in a different column in the following row.

 | Fruit  | Value1| Value2|
 --------------------------
 | APPLE  |  A    |       |
 | APPLE  |       | E     |
 | PEAR   |  A    |       |
 | PEAR   |       | X     |
 | FIG    |       | X     |
 | FIG    |  A    |       |
 | CHERRY |  A    |       |
 | CHERRY |       | X     |
 | CHERRY |  A    |       |
 | CHERRY |       | X     |
 | GRAPE  |       | X     |
 | GRAPE  |       | T     |
 | ORANGE |  A    |       |
 | ORANGE |       | X     |
 | ORANGE |       | Y     |
 | ORANGE |       | Z     |
 | PEACH  |  B    |       |
 | PEACH  |  A    |       |
 | PEACH  |       | X     |
 | MANGO  |  B    |       |
 | MANGO  |  C    |       |
 | MANGO  |  D    |       |

From the above table, I would like to select all rows for a given Fruit, where Value1 is A on one row, Value2 is X on the following row, and nothing other than A appears in Value1 on any row for that Fruit.

From the above table, the query should deliver results that look like this:

 | Fruit  | Value1| Value2|
 --------------------------
 | PEAR   |  A    |       |
 | PEAR   |       | X     |
 | CHERRY |  A    |       |
 | CHERRY |       | X     |
 | CHERRY |  A    |       |
 | CHERRY |       | X     |
 | ORANGE |  A    |       |
 | ORANGE |       | X     |
 | ORANGE |       | Y     |
 | ORANGE |       | Z     |
  • APPLE is excluded because on the row after the one where Value1=A,
    Value2!=X.
  • FIG is excluded because Value2=X occurs on the row before Value1=A,
    instead of the row after.
  • GRAPE is excluded because there is no row where Value1=A.
  • PEACH is excluded because there is at least one row where Value1!=A.
  • MANGO is excluded because there is no row were Value1=A, and because
    there is no row where Value2=X.

The part that seems a bit tricky to me is performing several checks at the level of the group of rows, but still returning all the rows of the matching group.

Thanks in advance for tips and suggestions. Let me know if you need me to clarify the question. The database is DB2 V10 on z/OS.

  • 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-17T12:17:26+00:00Added an answer on June 17, 2026 at 12:17 pm

    Except for the FIG (in your example), which requires an order column (id or whatever), the query bellow solves your problem:

    select * from fruits 
    where fruit not in (select fruit from fruits where value1 is not null and value1 <> '' and value1 <> 'A')
    and exists (select fruit from fruits f2 where f2.fruit = fruits.fruit and value2 = 'X')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure why this SQL query is not working. I'm new to SQL/PHP
I'm relatively new to SQL - I'm not sure if I'm doing this right.
Hi everyone I am new to StackOverflow and SQL. I am not sure how
i m not able to modify table in SQL server. i m new to
I have another SQL/access 2007 question that seems really basic but I'm not sure
I have come across a problem and am not sure which would be the
I currently have a SQL Server Reporting Services site which is not part of
I have a situation where I have a SQL table which pretty much 'parents'
I'm not sure if this is practically possible with SQL (I'm using MS SQL
I`m new to sql and have been stuck on the following issue for almost

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.