Select entries that have non repeating values on a specific column (although other columns may have repeating or non repeating values) (SQL) - sql

Let's say I have the following table:
A
B
C
D
a1
b1
c1
d1
a1
b1
c1
d2
a2
b2
c3
d3
a2
b2
c4
d3
I want to filter and see all four columns for entries that have the same value con column A but different on column C, so I get only this as a result:
A
B
C
D
a2
b2
c3
d3
a2
b2
c4
d3
I don't really care if values con columns B and D are the same or different, although I would like to have them in my table to do further analysis later.
Using the DISTINCT statement would give me all the columns as a result, as they all are different in some column, so that doesn't work for me.
I read some questions (like this one) and the answers recommended using the row_number() over(partition by...) clause, although the use they gave it doesn't quite fit my problem (I think), as it would also return the first row with a repeating value on column C.
Any ideas how this could be done?

You can use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.a = t.a and t2.c <> t.c
)
order by t.a;

You could use a self join
select t1.*
from t t1
join t t2 on t1.a=t2.a and t1.c<>t2.c

Related

SQL - looking for matches with one to many table

I have two tables, one with orders that looks something like this
Order ID
Coupon
BIN
O1
C1
B1
O2
C2
B3
Coupon
BIN
C1
B1
C1
B2
C2
B2
Now I want to check if the BINs in the first table are not present in the second table against the coupon.
How do I write a Redshift query for this?
For example, my output from the sample tables would be O2, C2, B3
select o.*
from order o
leff join coupon c on o.bin=c.bin and o.coupon=c.coupon
where c.bin is null
An anti-join will find the rows of the first table which BIN value is not present in the second one. For example:
select o.*
from o
leff join c on c.bin = o.bin
where c.bin is null

Inconsistent results with jsonb_array_elements_text() twice in the SELECT list

Why does the behavior of the query below change when the number of elements in the array changes?
The following snippet expands two arrays on the same query and has two different behaviors:
When the two arrays have the same number of elements, one row per
element is returned
When the two arrays have different number of
elements, it behaves like a CROSS JOIN
All of this executed in Postgres 9.5.2:
CREATE TABLE test(a text, b jsonb, c jsonb);
INSERT INTO test VALUES
('A', '["b1","b2"]', '["c1","c2"]'),
('B', '["b1","b2"]', '["c1","c2","c3"]');
SELECT a, jsonb_array_elements_text(b) b, jsonb_array_elements_text(c) c
FROM test;
Here is the result:
A b1 c1
A b2 c2
B b1 c1
B b2 c2
B b1 c3
B b2 c1
B b1 c2
B b2 c3
Here is what I would expect:
A b1 c1
A b1 c2
A b2 c1
A b2 c2
B b1 c1
B b2 c2
B b1 c3
B b2 c1
B b1 c2
B b2 c3
Combining multiple set-returning functions in the SELECT list is not in the SQL standard, where all set-returning elements go into the FROM list. You can do that in Postgres, but it used to exhibit surprising behavior before version 10, where it was finally sanitized.
All of this is not directly related to the datatype jsonb or the function jsonb_array_elements_text() - beyond it being a set-returning function.
If you want the Cartesian product, reliably and not depending on your version of Postgres, use CROSS JOIN LATERAL instead (requires at least Postgres 9.3):
SELECT t.a, jb.b, jc.c
FROM test t
, jsonb_array_elements_text(t.b) jb(b)
, jsonb_array_elements_text(t.c) jc(c)
ORDER BY t.a, ???; -- your desired order seems arbitrary beyond a
The comma in the FROM list (,) is basically short syntax for CROSS JOIN LATERAL here.
See:
What is the difference between LATERAL and a subquery in PostgreSQL?
Explanation for your actual question:
Why does the behavior of the query below change when the number of elements in the array changes?
What is the expected behaviour for multiple set-returning functions in SELECT clause?

SQL complex query for three fields

I have a table with this structure:
Field A Field B Field C
a1 b1 t1
a1 b1 t2
a2 b1 t1
a2 b1 t2
Field A and Field B are related, in a way that there are several a* for a given b*.
I need to list either Field A or Field B that meet the following criteria:
b* has several a*
a* has 1 to N t*
if two a* belonging to the same b* have different t*, then I need to list that a* or its parent b*
In the previous example, nothing whould be listed because b1 has a1 and a2, and both a1 and a2 have the same information: they both have t1 and t2.
In the next example, I need to detect that a1 and a2, belonging to b1, have different information (a1: t1 and t2; b1: only t1, it doesn't have t2).
Field A Field B Field C
a1 b1 t1
a1 b1 t2
a2 b1 t1
a3 b2 t3
The query would show either a1, a2 or b1.
I know this is complicated but I need to get that information.
Thanks!
You actually need to add the explanation to why there's nothing returned in the first example to the requirements.
Solution to example 1:
SELECT
t.B,
GROUP_CONCAT(DISTINCT t.A)
FROM
t
INNER JOIN (
SELECT
B,
A
FROM
t
GROUP BY B, A
HAVING
COUNT(DISTINCT C) > 1
) q ON t.A = q.A AND t.B = q.B /* a* has 1 to N t* */
WHERE t.B IN (
SELECT sq.B FROM (
SELECT st.B, GROUP_CONCAT(st.C ORDER BY st.C) AS gcc FROM t AS st GROUP BY st.B, st.A
) sq GROUP BY sq.B HAVING COUNT(DISTINCT gcc) > 1
) /* if two a* belonging to the same b* have different t*, then I need to list that a* or its parent b* */
GROUP BY t.B
HAVING
COUNT(DISTINCT t.A) > 1 /* b* has several a* */
;
play around with it if you want in this sqlfiddle
Your second example is not clear to me. But I guess you mean this:
SELECT
B, A
FROM
t
WHERE A IN ('a1', 'a2')
GROUP BY B, A
HAVING
COUNT(DISTINCT C) > 1
OR
COUNT(*) = 1;
sqlfiddle for Example 2

Finding Permutations of columns in SQL

I have a reference data table having columns as codes and values.
For e.g. there are three code types viz. A, B, C.
The table is as below:
Code Value
---------------------
A1 a_one
A2 a_two
B1 b_one
B2 b_two
B3 b_three
C1 c_one
C2 c_two
C3 c_three
C4 c_four
---------------------
I have a requirement where the input will be code types and output should be all permutations between the input code types.
For e.g. if the input code types are A and C, the output of my sql should be:
col_1 col_2
---------------------
A1 C1
A1 C2
A1 C3
A1 C4
A2 C1
A2 C2
A2 C3
A2 C4
---------------------
Similarly if the input code types is A, B, C, the output of the sql will have three columns with all the permutations between A, B, C viz. A1 B1 C1 to A2 B3 C4.
I have no idea how to start on this. So any hints will be useful.
Thanks for reading!
If I understand your question correctly, this is one of those rare cases where a CROSS JOIN is actually what you want. A CROSS JOIN will give you the Cartesian product of two sets, which means all possible combinations between the values in those sets.
Example:
Table A with column 1 contains values 'a' and 'b'
Table B with column 2 contains values 'c' and 'd'
The following CROSS JOIN query (note there is no 'join condition' specified, on purpose):
SELECT *
FROM A
CROSS JOIN B
will return the following result:
1 2
--------
a c
a d
b c
b d
I created an SQL Fiddle to show you a possible solution. You can tweak it a bit to see if this is what you need. (Note it's an Oracle fiddle, as there is no DB2 option.)

Compare Every Record from Two Tables

Assuming I have two SQL tables consisting of a single column,
i.e.
Table 1 Table 2
a1 a2
b1 b2
c1 c2
Is there a succinct SQL command to compare each record in one table against each record in the other? (and return true if any record from table 1 matches any record from table 2)
i.e.
if( a1 = a2 OR a1 = b2 OR a1 = c2 OR b1 = a2 OR b1 = b2...)
I want
If any record from table a matches table b (i.e., in a table of ints, they are the same int), return true.
Why not simply
if exists (select 1 from T1 inner join TB on T1.Col = T2.Col)
A full join is well suited to finding differences. To find rows that are missing in either table:
select *
from t1
full join
t2
on t1.col1 = t2.col1
where t1.col1 is null
or t2.col1 is null
This assumes that the single column is unique (i.e. has no duplicate values.)