Finding Permutations of columns in SQL - 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.)

Related

Get none null column row and other row when its null

I have a problem and I wondered if I can do it only with sql.
So I have a table named: tbl, and columns a, b, c which a and b rows aren't null and c can be null, for example:
a
b
c
a1
b1
NULL
a2
b2
c2
a3
b3
NULL
And I wish to get the result with columns a and x when x is defined: take from b when c is null and take from c if its not null, so the output will be:
a
col2
a1
b1
a2
c2
a3
b3
There is a sql command which can do the above?
Thanks
You can use the COALESCE function, given that your DBMS supports it.
SELECT a,
COALESCE(c, b)
FROM tab
If your DBMS does not support the functions from answers above, you can do it with CASE function:
SELECT
a,
CASE
WHEN c IS NULL THEN b
ELSE c
END AS col2
FROM
tbl
You can use
select a, nvl(c, b) from table;

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

Select entries that have non repeating values on a specific column (although other columns may have repeating or non repeating values) (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

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 - Merge / Update & Delete from existing rows

Given the following table:
A B C D E
a1 b1 NULL NULL e1
NULL NULL c1 d1 NULL
a1 b1 c1 NULL NULL
I want to run a query that would merge/update&delete the existing rows whenever at least one of the columns have a equal value (except when NULL = NULL) to result in the following table:
A B C D E
a1 b1 c1 d1 e1
Please note that there are no unique IDs in any of the columns as any could have NULL values.
Could you please assist?
Edited:
If any of the columns do not share the same value, I would them to be a separate record; and this would not affect any other records.
For example, if row 3 was:
A B C D E
a1 b1 c1 NULL e2
then the desired output would be:
A B C D E
a1 b1 NULL NULL e1
a1 b1 c1 d1 e2
since row 1 already has a e1 <> e2 (so is left as a separate record); and row 2 and 3 are merged since they share c1 as a common C value AND also does not have any columns with differing values (other than NULLS).
Another example:
If there was an additional row, row 4 (with the original table):
A B C D E
NULL NULL c1 d2 NULL
Then the desired output would be:
A B C D E
a1 b1 c1 d1 e1
a1 b1 c1 d2 e1
This isn't really a complete answer, but rather an answer that will help others understand the problem and hopefully help steer them in the right direction to provide the complete answer:
As I understand it, the fundamental problem with the scenario you describe is that we need something like TRANSACTION (but probably not TRANSACTION) which will somehow allow us to accomplish the following steps.
Step 1.
In the following table, take the first row and compare it to all the other rows.
A B C D E
a1 b1 NULL NULL e1
NULL NULL c1 d1 NULL
a1 b1 c1 NULL NULL
For the first row, we find a match with the third row in column [A] and then we update columns [B], [C], [D], and [E], then we delete the third row - giving us the following table:
A B C D E
a1 b1 c1 NULL e1
NULL NULL c1 d1 NULL
Step 2
We look at column [B] and find no matches.
Step 3
We look at column [C] and find a match so we update the current row (row 1). We are ignoring / not merging NULLs so we get the following table
A B C D E
a1 b1 c1 d1 e1
The problem with this is that MERGE operations do not work as described. By that I mean once a row is MERGED, we move onto the next row.
This is a fairly complicated process so I personally am not sure how to tackle it, just wanted to help explain the problem in a more "developer-friendly" way.
I would expect the answer to look something like this (I don't think you can actually use isnull in the ON clause):
MERGE Table2 AS target
USING Table1 as source
ON (isnull(target.[A], -1) = isnull(source.[A], -2) OR isnull(target.[B], -1) = isnull(source.[B], -2), etc.... )
WHEN MATCHED
THEN UPDATE
SET target.[A] = source.[A]
and so on...