Merge statement to remove duplicate values - google-bigquery

I am trying to remove the duplicate entries as following
Table abc
id name service_file_id
1 abc xyz
2 vbg xyz
2 vbg xyz
3 kio xyz
3 kio xyz
4 abc yzx
5 nji yzx
6 pop yzx
Table temp_table
service_file_id file_id
xyz null
yzx file_name_1
The table abc has duplicate entries for example
id name service_file_id
2 vbg xyz
2 kio xyz
I get the information about the duplicate entries via temp table. When a file_id in temp table is null, it means I have duplicate entries for that particular serivce_file_id
Below is the code I have tried
merge `abc` t
using (
# choose a single row to delete the duplicates
SELECT distinct a.*
FROM `abc` a
left join `temp_table` b
on a.service_file_id = b.file_id
where b.file_id is not null
)
ON FALSE
WHEN NOT MATCHED BY SOURCE THEN DELETE
WHEN NOT MATCHED BY TARGET THEN INSERT ROW
I can remove the duplicate entries from the table but it also deletes the rest of the data which is not duplicate.
current output
id name service_file_id
1 abc xyz
2 vbg xyz
3 kio xyz
Desired output
id name service_file_id
1 abc xyz
2 vbg xyz
3 kio xyz
4 abc yzx
5 nji yzx
6 pop yzx
Could you please help me out here?
Thanks in advance!!

Without actually referencing the temp_table table you can delete the duplicates in your abc table just by using merge and select distinct :
merge `abc` t
using(
SELECT distinct a.*
FROM `abc` a
)
ON FALSE
WHEN NOT MATCHED BY TARGET THEN INSERT ROW
WHEN NOT MATCHED BY SOURCE THEN DELETE
Output:

Related

To update CLOB column in Oracle

I need to update table B with column Value with CLOB type from table A
Table A
ID Value
1001 ABC
1002 CDE
1003 ABC
1004 PWD
Table B to be updated as below:
ID - varchar2(355)
Value - CLOB
ID Value
ABC 1001!1003
CDE 1002
PWD 1004
Looks more like an INSERT, not UPDATE. Anyway, LISTAGG will help in both cases.
SQL> insert into b (id, value)
2 select a.value, listagg(a.id, '!') within group (order by a.id)
3 from a
4 group by a.value;
3 rows created.
SQL> select * From b;
ID VALUE
---------- --------------------------------------------------
ABC 1001!1003
CDE 1002
PWD 1004
SQL>

Select records from a table where two columns are not present in another table

I have Table1:
Id Program Price Age
12345 ABC 10 1
12345 CDE 23 3
12345 FGH 43 2
12346 ABC 5 4
12346 CDE 2 5
12367 CDE 10 6
and a Table2:
ID Program BestBefore
12345 ABC 2
12345 FGH 3
12346 ABC 1
I want to get the following Table,
Id Program Price Age
12345 CDE 10 1
12346 CDE 2 5
12367 CDE 10 6
I.e get the rows from the first table where the ID+Program is not in second table. I am using MS SQL Server express 2012 and I don't want to add any columns to the original databases. Is it possible to do without creating temporary variables?
Several ways to do this, here's one using not exists:
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
One possible variant is to use LEFT JOIN:
SELECT
Table1.*
FROM
Table1
LEFT JOIN Table2
ON Table1.ID = Table2.ID
AND Table1.Program = Table2.Program
WHERE
Table2.ID IS NULL

How to join two tables based on ID and get information

Hello I need some help regarding SQL Query.
Table A
ID Name Type
---------------------
1 abc BC
---------------------
2 def SD
---------------------
3 ghi BC
----------------------
Table B (BC_ID and SD_ID are Foreign keys of Table 'B' referencing the Primary key 'ID' of Table 'A')
ID BC_ID SD_ID
---------------------
1 1 2
---------------------
2 3 2
---------------------
I am using VB.Net datagrid view to print the Table B details. I need to print 2 more columns in datagridview which specifies the Name of the corresponding BC_ID and SD_ID.
How do I write a SQL query which can get the 'Name' of the Corresponding ID's from the database and print in datagridview...
Expected Output in datagridview:
ID BC_Name SD_Name
---------------------
1 abc def
---------------------
2 ghi def
---------------------
I dont want to have separate BC_Name and SD_Name columns in the table B..I just need to join the two tables and get the name in a single query..Is it possible? Thanks in advance for your help
You need to join TableA twice with different alias names.
Try this:
SELECT B.ID,ISNULL(A1.NAME,'') as BC_NAME,ISNULL(A2.NAME,'') as SD_NAME
FROM TableB B LEFT JOIN
TableA A1 ON A1.ID=B.BC_ID LEFT JOIN
TableA A2 ON A2.ID=B.SD_ID
Result:
ID BC_NAME SD_NAME
1 abc def
2 ghi def
See result in SQL Fiddle.

Executing Insert into statement in SQL resulting in nulls for other columns

i am trying to use an insert into a column where the data for other columns already exists, but the data is not populating adjacent to the other columns, instead data is inserted after all the data in the table.
For example:
select * from tab1;
ID NAme Last_name
1 King
2 Queen
3 Rook
select * from tab2;
Id LastName_Name
1 Abc
2 def
3 xyz
SQL : Insert into tab1 (Last_name)
select tab2.LastName_Name from tab1,tab2, where tab1.Id=tab2.Id
Output:
Id Name Last_Name
1 King NULL
2 Queen NULL
3 Rook NULL
4 NULL Abc
5 NULL def
6 NULL xyz
But I want the data as below:
Id Name Last_Name
1 King Abc
2 Queen def
3 Rook xyz
Any work around for this? thanks in advance :)
Step2:
select * from tab1;
ID Name Id2
1 King NA
2 Queen NA
3 Rook NA
select * from tab2;
ID
1
2
3
4
5
6
I want the Output data as below:
The ID data in tab2 should populate in tab1 column (ID2) which are matching with TAB1.ID column values as below:
Id Name ID2
1 King 1
2 Queen 2
3 Rook 3
Can you please provide any query for this?
So you are wanting to UPDATE the rows in tab1 with the corresponding last names from tab2?
In which case, use an UPDATE statement instead of an INSERT:
UPDATE tab1
SET tab1.Last_name = tab2.LastName_Name
FROM tab1
JOIN tab2 ON tab1.Id = tab2.Id
You don't need an INSERT you need an UPDATE statement:
UPDATE tab1 SET tab1.Last_name = tab2.LastName_Name
FROM tab1 INNER JOIN tab2 ON tab1.Id = tab2.Id

confusing query

Hi I have 3 table xyz abc and pqr,structure of xyz and abc is same ,but the query inside exists clause is confusing me ,why someone put the table pqr when there is no need of that ,even no joining is been done here with that table .
Insert into xyz
select * from abc where exist (select 1 from pqr where abc.pk_id =1234)
abc.pk_id is primary key of table xyz
Note:I have not written this query this is existing in production ,please reply.
A slightly less confusing version of the same query might be:
Insert into xyz
select * from abc where abc.pk_id = 1234 and exists (select 1 from pqr)
In other words, insert records from abc for the specified pk_id, when pqr is not empty.
there is no need for pqr table in this context the following query should do the same:
Insert into xyz
select * from abc where pk_id =1234