Getting Unique result set from two table in SQL Server - sql

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))

Related

using if condition in snowflake

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;

How to get more column values along with distinct column in the Oracle database?

How to get more columns along with distinct column values in Oracle ?
select DISTINCT cname
from customer
where code is not null;
I need cname, cvalue, cdate with distinct cname
Your question does not really make sense. DISTINCT does not affect columns, it eliminates duplicate rows. So if you have:
a b c
a b c
d b c
Using DISTINCT gives you:
a b c
d b c
You're going to have to be more specific in the output you're getting and what you want.

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.

Merge statement help in oracle

I am using merge for the 1 st time ... I went through existing questions but couldn't get proper help.
Please help me with the below need,
I have a table "table_a" with 3 columns A, B and C. C is a new column added combination of column A and B are unique, to be specific column B is a list of sub codes taken from table_b and configured against the entity in column A.
I need to update column C with a hard coded value for the existing A and B combinations and if some subcode missing from table_b in table_a I need to insert the rows for the same in table_a.
eg. table_a
A B C
= = =
p x
p y
table_b
M
=
x
y
z
After execution of query
table_a
A B C
= = =
p x 1 -- updated with column C
p y 1 -- updated with column C
p z 1 -- new row inserted for the row in table_b
Kindly let me know if anything is not clear.
MERGE INTO table_A a
USING table_b b
ON(a.b = b.m)
WHEN MATCHED THEN
UPDATE SET a.c = 1
WHEN NOT MATCHED THEN
INSERT (a, b, c)
VALUES ('P', b.m, 1)
Note : The insert has two hardcode values for column a and column c as 'P' and 1 respectively.
If you don't want to hard-code the inserted values you can use an in-line view to generate all the expected combinations and merge against that.
merge into table_a a
using (
select t.a, b.m, 1 as c
from (select distinct a from table_a) t
cross join table_b b
) b
on (a.b = b.m)
when matched then
update set a.c = b.c
when not matched then
insert (a, b, c) values (b.a, b.m, b.c);
The using clause does a cross-join of all (distinct) table_a.a values against all table_b.m values, to give all possible combinations. (Which seems to be what you want; you haven't shown any other link between the two). I've also included the fixed value 1 in that view.
Then the merge either sets c for the rows the matching values that already exit in table_a, or inserts a new row using the values from the in-line view.
SQL Fiddle.
You might be able to get your unique a values from some other look-up table, which would be better than hitting table_a twice, but that depends on your real data model.

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?