select * from tbl_a;
SRNO T_TEXT
1 a
1 b
1 c
2 a
2 b
3 a
3 b
4 a
----
select * from tbl_b;
SRNO T_TEXT
1
1
1
2
2
3
3
4
i want to update tbl_b.t_text with values from tbl_a.t_text.
i do that it gives returned too many rows. I can do this trough a for loop,
but want it through update statement only.
Here is the SQL I've tried#
update tbl_b b
set b.t_text = (select a.t_text from tbl_a a
where a.srno = b.srno and b.t_text is null)
where exists ( select 1 from tbl_a c
where c.srno = b.srno);
It throws error single row subquery return more than one row.
You can use this code to update tbl_b
begin
for i in (select a.* from tbl_a a) loop
update tbl_b b
set b.t_text=i.t_text
where b.srno=i.srno
and b.t_text is null
and rownum=1;
end loop;
end;
I wish it helps you.
you can use Below query
UPDATE tbl_a
SET tbl_a.T_TEXT=(SELECT tbl_b.T_TEXT
FROM tbl_b
WHERE tbl_b.SRNO=tbl_a.SRNO);
Related
I'm trying to get all ids from one table and a count of transactions from another table. The trick is that an id may not be listed in the transaction table. In that case, I want the query to return 0 for that id. (I apologize for the bad formatting)
ID Table
ID
1
2
3
Trans Table
ID Trans
1 123
1 234
3 345
3 456
3 567
Query results
ID - Trans Count
1 2
2 0
3 3
I have this code, but it just isn't working for me and I can't figure out why.
SELECT A.ID, COUNT (B.TRANS) AS CNT
FROM A
LEFT JOIN B
ON A.ID = B.ID
WHERE B.DTE BETWEEN '01-Mar-2017' AND '31-Mar-2017' AND
A.CURRENT_FLAG = 1
GROUP BY A.ID
When using left join, conditions on the first table go in the where clause. Conditions on the second table go in the on clause:
SELECT A.ID, COUNT (B.TRANS) AS CNT
FROM A LEFT JOIN
B
ON A.ID = B.ID AND
B.DTE BETWEEN '01-Mar-2017' AND '31-Mar-2017' AND
WHERE A.CURRENT_FLAG = 1
GROUP BY A.ID;
I would check if the value is null if then just replace it with a 0
The NVL Function will work perfect for this scenario.
SELECT A.ID, COUNT (NVL(B.TRANS,0)) AS CNT
FROM A
LEFT JOIN B
ON A.ID = B.ID
WHERE B.DTE BETWEEN '01-Mar-2017' AND '31-Mar-2017' AND A.CURRENT_FLAG = 1
GROUP BY A.ID
I have a table old_data and a table new_data. I want to write a select statement that gives me
Rows in old_data stay there
New rows in new_data get added to old_data
unique key is id so rows with id in new_data should update existing ones in old_data
I need to write a select statement that would give me old_data updated with new data and new data added to it.
Example:
Table a:
id count
1 2
2 19
3 4
Table b:
id count
2 22
5 7
I need a SELECT statement that gives me
id count
1 2
2 22
3 4
5 7
Based on your desired results:
SELECT
*
FROM
[TableB] AS B
UNION ALL
SELECT
*
FROM
[TableA] AS A
WHERE
A.id NOT IN (SELECT id FROM [TableB])
I think this would work pretty neatly with COALESCE:
SELECT a.id, COALESCE(b.count, a.count)
FROM a
FULL OUTER JOIN b
ON a.id = b.id
Note - if your RDBMS does not contain COALESCE, you can write out the function using CASE as follows:
SELECT a.id,
CASE WHEN b.count IS NULL THEN a.count
ELSE b.count END AS count
FROM ...
You can write a FULL OUTER JOIN as follows:
SELECT *
FROM a
LEFT JOIN b
ON a.id = b.id
UNION ALL
SELECT *
FROM b
LEFT a
ON b.id = a.id
You have to use UPSERT to update old data and add new data in Old_data table and select all rows from Old_data. Check following and let me know what you think about this query
UPDATE [old_data]
SET [count] = B.[count]
FROM [old_data] AS A
INNER JOIN [new_Data] AS B
ON A.[id] = B.[id]
INSERT INTO [old_data]
([id]
,[count])
SELECT A.[id]
,A.[count]
FROM [new_Data] AS A
LEFT JOIN [old_data] AS B
ON A.[id] = B.[id]
WHERE B.[id] IS NULL
SELECT *
FROM [old_data]
I've a SQL DB table ABC, in that I've two columns i.e. column1 and column2.
In this table I have some data like.
column1 column2
-------------------
1 2
1 7
2 1
3 4
7 1
4 3
Now, I have to delete the data from this table which are cross linked to each other. for e.g.
(1,2) are cross linked to (2,1)
(1,7) are cross linked to (7,1)
(3,4) are cross linked to (4,3)
So, I need to delete one of value from this pair. My final output should be like:
column1 column2
-------------------
1 2
1 7
3 4
OR
column1 column2
-------------------
2 1
4 3
7 1
I want to write a sql query to do this. Anyone has any idea how can I achieved this?
Try this:
SQLFIDDLE
with pairs as (select
case when c1< c2 then c1 else c2 end as minc,
case when c1< c2 then c2 else c1 end as maxc
from t
group by
case when c1< c2 then c1 else c2 end ,
case when c1< c2 then c2 else c1 end
having count(*) >1)
select *
from t
where not exists
(select * from pairs
where c1= minc and c2= maxc
)
Explain
The CTE table returns all paired rows of one side.
Through NOT EXISTS, it returns all rows not paired
If you change the condition of where c1= minc and c2= maxc to where c2= minc and c1= maxc will get the opposite side of the pairs.
If you want delete one side of those pairs, with DELETE FROM T WHERE EXISTS instead of NOT EXISTS
There have some different ways to get paired rows.
SELECT A.* FROM test A LEFT JOIN test B
ON A.column1 = B.column2 AND A.column2 = B.column
WHERE B.column IS NULL;
This should work, assuming your OK with something like (2,2) also being excluded.
i have this query:
update B
set B.NBR_OF_BACKUP=B.NBR_OF_BACKUP - 1
FROM BACKUP_TABLE B
INNER JOIN tbl I ON B.ID_BACKUP=i.id_backup
in the table tbl i have:
ID_BACKUP ID_IMAGES
1 2
1 3
1 4
6 3
my query is updating only for distinct id_backup
but i need also to update the table BACKUP_TABLE as many times as the same id_backup is in the table tbl
I think what you want is this:
update B
set B.NBR_OF_BACKUP=I.NBR_OF_BACKUP - 1
FROM BACKUP_TABLE B
INNER JOIN (SELECT id_backup, count(*) as nbr_of_backup FROM tbl GROUP BY id_backup) I
ON B.ID_BACKUP=i.id_backup
I have a huge stored procedure and I am trying to optimize it .
I have a temporary table A , with column Id.
I have a main table B with columns Id & a boolean field Test
For all Id's in Table A , I need to make the Test = 1 in Table B.
example :
A
Id
--
1
2
3
I need to get table B as follows.
I already have the Id field in Table B , I only need to update the
Test Column
B
Id Test
-- ----
1 1
2 1
3 1
4
5
6
I am currently looping through Table A using a while loop and for each Id
I am updating the Test column in Table B .
WHILE (1 = 1)
BEGIN
-- Exit loop if no more Transactions
IF ##ROWCOUNT = 0 BREAK;
Update [B]
set Test = 1
where Id = (SELECT TOP 1 Id
FROM #B
WHERE Id > #Id1
ORDER BY Id)
END
PS : #Id1 is the input parameter of the stored procedure.
I cannot think of any other efficient way of doing it, but
my query is taking a lot of time to run..
Please let me know if there is a better way of doing the same thing.
This is pretty straight forward.
UPDATE [B]
SET [TEST] = 1
FROM [B] INNER JOIN [A]
ON [A].ID = [B].ID
Another alternative:
UPDATE b SET test = 1
FROM dbo.TableB AS b
WHERE EXISTS (SELECT 1 FROM dbo.TableA WHERE ID = b.ID);
You don't need to loop for this, just join those tables
UPDATE B
SET B.Test = 1
FROM TableB B
INNER JOIN TableA A
ON A.Id = B.ID
Here is an sqlfiddle with a live demo.
You can use in to find the records to update:
update b
set Test = 1
where Id in (select Id from #A)
Another alternative is to join the tables:
update x
set Test = 1
from b as x
inner join #A as a on a.id = x.id
If I'm understanding your question correctly, all you need to do is join A and B.
UPDATE TableB SET test = 1
FROM TableB b
INNER JOIN TableA a ON a.id = b.id
WHERE b.ID > #Id1
Basically you're looking to update everywhere where TableA and TableB intersect.
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html