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
Related
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:
I like to do a matched on one column and update the value for another column between two tables. let's me demo how I like it to get updated.
Example:
-- I have Table1 and Table2 below
Table1:
Name Number
--------------
abc 1111
abc 2222
abc 3333
xyz 4444
xyz 5555
xyz 6666
Table2:
Name Number
-------------
abc 9999 (already exists, before updated)
abc NULL
abc NULL
abc NULL
abc NULL
abc NULL
abc NULL
xyz NULL
xyz NULL
xyz NULL
xyz NULL
xyz NULL
xyz 8888 (already exists, before updated)
I want to do a match between Table1 and Table2, and update Table2 for matched names that are blank (NULL), and just the first matched records in Table1. As you can see, "abc" has only 3 records need to be updated, and you see only the first 3 blank (NULL) in Table2 get updated.
Table2 (after updated)
Name Number
--------------
abc 9999 (already exists, before updated)
abc 1111
abc 2222
abc 3333
abc NULL
abc NULL
abc NULL
xyz 4444
xyz 5555
xyz 6666
xyz NULL
xyz NULL
xyz 8888 (already exists, before updated)
I am not sure if this is possible. Please help.
Thanks,
; WITH tbl1 as (
SELECT Name, Number, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Number) rn
FROM Table1)
,tbl2 as (
SELECT Name, Number, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Number) rn
FROM Table2
WHERE Number IS NULL)
UPDATE tbl2
SET Number = tbl1.Number
FROM tbl2
INNER JOIN tbl1 ON tbl2.Name = tbl1.Name AND tbl2.rn = tbl1.rn
I cannot remember exactly how to use it, and I don't have an example until I get to work again.. But you can achieve this by using a cursor and selecting out a single row at a time and updating it based on the data from a specific row in table 1..
See the help site on msdn: http://msdn.microsoft.com/en-us/library/ms180169.aspx
UPDATE:
I have now constructed a small piece of code that will do what you are asking ;-) See below.
What the cursor does, is to retrieve both name and number from table1, one by one. The when you have fetched this, it will then update table two where the name matches the fetched name from table one AND where the number in table two is NULL.
DECLARE #name nvarchar(max)
DECLARE #number int
DECLARE name_cursor CURSOR FOR
SELECT name, number
FROM #table1
OPEN name_cursor
FETCH NEXT FROM name_cursor
INTO #name, #number
WHILE ##FETCH_STATUS = 0
BEGIN
UPDATE top(1) #table2
SET number = #number
FROM #table2
WHERE
name = #name
and number is null
FETCH NEXT FROM name_cursor
INTO #name, #number
END
CLOSE name_cursor
DEALLOCATE name_cursor
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.
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
What I'm trying to do is return all the records my persons table that do not exist in secondary table or that do exist, but only if one they have null values in one of two specific fields. So given the following tables
person_id ....
----------------
123 ....
456 ....
789 ....
p_id colA colB
--------------------
456 aadl
789 023j ljlj
I would get back the following
person_id ....
----------------
123 ....
456 ....
This would be because person 123 does not exist in the secondary table. Person 456 would also be returned because they exist, but one of the two columns are null.
Thanks!
You could write:
SELECT *
FROM persons
WHERE person_id NOT IN
( SELECT p_id
FROM secondary_table
WHERE colA IS NOT NULL
AND colB IS NOT NULL
)
;