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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:03:22+00:00 2026-05-14T22:03:22+00:00

Can anyone help me with a SQL query in Apache Derby SQL to get

  • 0

Can anyone help me with a SQL query in Apache Derby SQL to get a “simple” count.

Given a table ABC that looks like this…

    
    id  a   b   c
    1   1   1   1
    2   1   1   2
    3   2   1   3
    4   2   1   1
**  5   2   1   2 **
**  6   2   2   1 **
    7   3   1   2
    8   3   1   3
    9   3   1   1

How can I write a query to get a count of how may distinct values of ‘a’ have both (b=1 and c=2) AND (b=2 and c=1) to get the correct result of 1. (the two rows marked match the criteria and both have a value of a=2, there is only 1 distinct value of a in this table that match the criteria)

The tricky bit is that (b=1 and c=2) AND (b=2 and c=1) are obviously mutually exclusive when applied to a single row.
.. so how do I apply that expression across multiple rows of distinct values for a?

These queries are wrong but to illustrate what I’m trying to do…
SELECT DISTINCT COUNT(a) WHERE b=1 AND c=2 AND b=2 AND c=1 ...
.. (0) no go as mutually exclusive
SELECT DISTINCT COUNT(a) WHERE b=1 AND c=2 OR b=2 AND c=1 ...
.. (3) gets me the wrong result.
SELECT COUNT(a) (CASE WHEN b=1 AND c=10 THEN 1 END) FROM ABC WHERE b=2 AND c=1
.. (0) no go as mutually exclusive

Cheers,
Phil.

  • 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-14T22:03:22+00:00Added an answer on May 14, 2026 at 10:03 pm

    I’m assuming that (a,b,c) is unique. One way to do this is to use a self join:

    SELECT COUNT(*)
    FROM   ABC T1
    JOIN   ABC T2
    ON     T1.a = T2.a
    WHERE  T1.b = 1 AND T1.c = 2
    AND    T2.b = 2 AND T2.c = 1
    

    This works conceptually as follows:

    • Find all the rows that satisfy (b,c) = (1,2)
    • Find all the rows that satisfy (b,c) = (2,1)
    • Join the above two sets on when a is the same.
    • Count the number of rows in the joined result.

    An alternative way which might be easier to understand is to use a subselect:

    SELECT COUNT(*)
    FROM   ABC
    WHERE  a IN (SELECT a FROM ABC
                 WHERE  b = 2
                 AND    c = 1)
    AND    b = 1
    AND    c = 2
    

    Note: If there can be duplicated values of (a,b,c) then instead of SELECT COUNT(*) use SELECT COUNT(DISTINCT T1.a) in the first query, and SELECT COUNT(DISTINCT a) in the second.

    These queries are tested in MySQL, not Apache Derby, but I hope they will work there too.

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

Sidebar

Related Questions

Please can anyone help me in changing the below T-SQL query to NHibernate query:
Can anyone help me, to convert the below sql query to Linq (Lambda Expressions).
I built this query, but I get a weird error, can anyone help me
I wonder if anyone can help improve my understanding of JOINs in SQL. [If
I'm new to querying XML datatype in SQL Server 2005. Anyone can help me
Can anyone help me to get good WordPress interview questions and answers. Any link
Can anyone help me out with this query i m trying to execute, static
Can anyone help me with SQLite query in my iOS app? I want to
Can anyone help - this is driving me mad. I am calling a mysql
Can Anyone help me why x2 prints zero. I guess because of floating point

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.