P
P
pqgg7nwkd42020-08-20 11:54:15
PostgreSQL
pqgg7nwkd4, 2020-08-20 11:54:15

How to determine that three predicates are equal to each other by specifying them once in an expression?

The truth table is:

A	B	C	?
1	1	1	1
1	1	0	0
1	0	1	0
1	0	0	0
0	1	1	0
0	1	0	0
0	0	1	0
0	0	0	1

Истина, когда либо все аргументы - истины, либо когда все аргументы - лжи.

Where, A, B, C are predicates - expressions that take the values ​​true or false.
We need a formula for the table above, in which A, B, C are present once .
It is desirable that the formula be scalable to 4 or more values.
The simpler the formula, the better.
Language: postgresql (version 12), you cannot create functions, you can use standard functions and extensions that come with the error and are added using CREATE EXTENSION.

Here are some solution examples:
(SELECT bool_and(a) = bool_or(a) FROM unnest(ARRAY[A, B, C]) a(a))

A::int + B::int + C::int IN (0, 3)


Maybe there are ideas in a simpler, better way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2020-08-20
@Stalker_RED

CREATE EXTENSION intarray;
1. sort( ARRAY[4,3,2,1] ); -- place the values ​​in an array, sort it
2. compare the first and last element, if they are equal, then the rest are all equal
Example:

select arr[1] = arr[array_upper(arr, 1)] as isEqual
  from sort(ARRAY[1,87,5,456,34,1]) as arr;  -- false

select arr[1] = arr[array_upper(arr, 1)] as isEqual
  from sort(ARRAY[1,1,1]) as arr;  -- true

select arr[1] = arr[array_upper(arr, 1)] as isEqual
  from sort(ARRAY[0,0,0,0]) as arr;  -- true

https://rextester.com/WKFL32015

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question