using if condition in snowflake - sql

I have two tables as shown below with columns:
Table A
a, b, c, id
Table B
d, e, f, g, h, id
Now I need perform a query basically I will get a id from user, so I need to check if that id is present in table A or table B. So the record will be present in any of one table
SELECT * FROM tableA WHERE id = 123
OR
SELECT * FROM tableB WHERE id = 123
So the response will be either columns of tableA or columns of tableB. But I can't perform a union since the columns should be equal among two tables.
So it's basically a if condition, how can I get the desired output in Snowflake.
And using if is the best optimized approach or any other way is there

You can use union all -- assuming the types are compatible. Just pad the smaller table:
select a, b, c, null as g, null as h, id
from a
where id = 123
union all
select d, e, f, g, h, id
from b
where id = 123;
If you want the columns separated, then a full join accomplishes that:
select *
from a full join
b
using (id)
where a.id = 123 or b.id = 123;

Related

Three-Way Diff in SQL

I have three SQL tables (A, B, and C), representing three different version of a dataset. I want to devise an SQL query whose effect is to extract the ids of rows/tuples whose values are different in all three tables. (Two records are different if there exists a field where the records do not share the same value.) For simplicity, let's just assume that A, B, and C each have N records with record ids ranging from 1 to N, so for every id from 1 to N, there is a record in each table with that ID.
What might be the most efficient way to do this in SQL? One way would be to do something like
(SELECT id FROM
((SELECT * FROM A EXCEPT SELECT * FROM B) EXCEPT (SELECT * FROM C)) result)
EXCEPT
(SELECT id FROM
((SELECT * FROM A) INTERSECT (SELECT * FROM B)) result2)
Basically what I've done above is first found the ids of records where the version in A differs from the version of B and from the version in C (in the first two lines of the SQL query I've written). What's left is to filter out the ids of record where the version in B matches the version in C (which is done in the last two lines). But this seems horribly inefficient; is there a better, more concise way?
Note: I'm using PostgreSQL syntax here.
I would do it like this:
select id,
a.id is null as "missing in a",
b.id is null as "missing in b",
c.id is null as "missing in c",
a is distinct from b as "a and b different",
a is distinct from c as "a and c different",
b is distinct from c as "b and c different"
from a
full join b using (id)
full join c using (id)
where a is distinct from b
or b is distinct from c
or a is distinct from c
The id column is assumed to be a primary (or unique) key.
Online example
You can use the group by and having as follows:
select id from
(select * from A
union select * from B
union select * from C)
group by id
-- use characters that you know will not appear in this columns for concat
having count(distinct column1 || '#~#' || column2 || '#~#' || column3) = 3

union all two table instead of join

I have several table which I can not join them as it gets really complicated and bigquery is not able to process it. So I am trying to union all tables and then group by. I have an issue during this process. I have two tables called t1 and t2 with below headers, they don't have null values:
a. b. c. d. a. b. c. e.
so in order to union all and group them I have below code:
WITH
all_tables_unioned AS (
SELECT
*,
NULL e
FROM
`t1`
UNION ALL
SELECT
*,
NULL d
FROM
`t2` )
SELECT
a,
b,
c,
MAX(d) AS d,
MAX(e) AS e
FROM
all_tables_unioned
GROUP BY
a,
b,
c
unfortunately when I run this I get a table a,b,c,d,e which e column is all null!
I tried to run query for each table before union all to make sure they are not null. I do not really know what is wrong with my query.
union all does not go by column names. Just list all the columns explicitly:
WITH all_tables_unioned AS (
SELECT a, b, c, d, NULL as e
FROM `t1`
UNION ALL
SELECT a, b, c, NULL as d, e
FROM `t2`
)
Regardless of the names you assign, the union all uses positions for matching columns.

Oracle SQL to subtract 2 values from different table joins

I am trying to subtract sequences MN_SEQ from Table C generated based on join with other tables.
Here is the problem.
Query 1 -
Select M_Seq from Table C, Table A, Table B where C.date_sk=A.MTH_END_DT
and B.Loan_seq=A.Loan_seq
Query 2 -
Select M_Seq from Table C, Table B where C.date_sk=B.ORIG_DT
I have to get difference between 2 M_SEQ generated from the result set of query 1 and Query 2.
Below is what i tried, but I am getting error.
select mn_seq -mn_seq from
((select mn_seq from Table C, Table A, Table B where B.MTH_END_DT=C.DATE_SK and B.LOAN_SEQ=A.LOAN_SEQ)a,
(select mn_seq from Table C , Table B where B.ORIG_DT=C.DATE_SK
)b)
T
Kindly provide inputs . I am not sure if this is the right way to do it. I tried just using "-" between queries but didnt work. Thanks!
Try this..
SELECT (SELECT mn_seq
FROM TABLE c, TABLE a, TABLE b
WHERE b.mth_end_dt = c.date_sk
AND b.loan_seq = a.loan_seq) -
(SELECT mn_seq FROM TABLE c, TABLE b WHERE b.orig_dt = c.date_sk)
FROM dual
I assume both the mn_seq are NUMBER and also your WHERE clause returns only one record in each of the inner queries.

Getting Unique result set from two table in SQL Server

I m new to SQL Server, please help me with the solution to following problem.
I have two tables which have the following columns:
Table-A only has one column with data like (A, B, C, D)
Table-B is a temporary table which can contain (B, B, C, C, E, E, F, G)
I need to insert contents of table B into table A.
Conditions are
if table A already contains any alphabet in B then that alphabet should be ignored.
Only a unique alphabet needs to be inserted into table A. i.e. Only one E can be inserted from two set of E and
if F and G also needs to be inserted.
How can I possibly solve this problem?
Thanking you in advance....
Try this....
INSERT INTO Table (Column)
SELECT DISTINCT tt.Column
FROM TempTab tt
WHERE tt.Column NOT IN (SELECT DISTINCT Column FROM Table)
Here is the SQL FIDDLE with this query.
INSERT INTO A(col)
(SELECT distinct col from B
minus
(SELECT distinct col FROM A INTERSECT SELECT distinct col FROM B))

SQL Server 2005: Using select in case statement

I have a temporary table whose data is populated from another table using select.
For eg.,
Table1's fields:
A, B, C
Table2's fields
A, D, E --A is foreign key from TableA
#sometemp's fields
X, Y, Z
insert into #sometemp (X, Y, Z)
select D, E, case
when (Table1's C is 0) then 0
when (Table1's C is 1) then somefunction(#arg1, #arg2)
end
from Table2
I cant figure it out how Table1's C value can be checked in the when clauses. Any ideas?