derby - SQL aggregate query question -


Can someone help me in SQL query in Apache Derby SQL to get a "simple" count?

Looking at a table ABC, it looks like this ...

How to count the specific value of 'a' I can write (B = 1 and C = 2) and (B = 2 and C = 1) to get the correct result. (Two rows match the marked criteria and the value of both is one = 2, there is only 1 different value

The hard bit is that (b = 1 and c = 2) AND (b = 2 and c = 1) Obviously when mutually exclusive is applied in a row .. So how can I express different values ​​for multiple lines of expression for one expression?

These questions are wrong but to clarify what I am trying to do ...
SE LECT DISTINCT COUNT (A) where B = 1 and C = 2 and B = 2 and C = 1 ... .. .. (0) do not go as mutually exclusive
SELECT DISTINCT COUNT (A) where B = 1 and C = 2 or B = 2 and C = 1 ... .. .. (3) I get wrong results.
SELECT COUNT (A) From ABC where B = 2 and C = 1 .. (0) not known as mutually exclusive

Cheers, Phil.

< P>

I'm assuming that (A, B, C) is unique. One way to do this is to use self joining. :

  ABC T Join ADC T1 1 (*) ABC T2 on T-1. A = T2. A where T1b = 1 and T1 C = 2 and T2.b = 2 and T2.c = 1  

works conceptually as follows:

  • All the rows Find all the rows that (b, c) = (1,2)
  • Find all rows that (b, c) = (2.11)
  • when one is equal, then join the two sets given above.
  • Calculate the number of rows in the associated result.

An alternative method may be that one from ABC (where one select ABC from B = 2 and C = 1) and B = 1 and C = 1 to <2 < / Code>

Note: If can be repeated instead of (A, B, C), instead use SELECT COUNT (*) SELECT COUNT (Secondary T1.a) In the first question, and SELECT COUNT (DISTINCT a) in another

These questions are tested IN MySQL, not Apache derby , But I hope they will work there too.


Comments