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
Related
I've got a resultset that looks something like:
[DateField ][Hour][Value]
2014-10-01 1 200
2014-10-01 2 600
I need to add a couple of additional columns from another table like:
[DateField ][Hour][Value][T2Value1][T2Value2]
2014-10-01 1 200 Off 5
2014-10-01 2 600 Off 7
I need the T2Value1 and T2Value2 from Table 2 below if the Value field from Table 1 falls between the RangeValue1 and RangeValue2 of Table 2.
[Id][T2Value1][T2Value2][RangeValue1][RangeValue2]
1 Off 5 1 500
2 Off 7 501 1000
I've started to query
select datefield, hour, value, t2value1, t2value2 from
(
-- inner query that returns datefield, hour, value
) Table1, Table2
but don't know where to take it from here. Would appreciate any help, thanks!
See this simple example:
declare #Values as table
(
Name varchar(10) not null
,Value int not null
)
declare #Ranges as table
(
Grade char(1) not null
,v0 int not null
,v1 int not null
)
insert into #Values
values
('Albert', 33)
,('Bob', 133)
,('Carl', 233)
insert into #Ranges
values
('A',0,100)
,('B',100,200)
,('C',200,300)
select * from #Values v
join #Ranges r on r.v0 <= v.Value and v.Value < r.v1
As you can see it's ok to put some more complex condition on the join clause, in this case a range condition
it results in:
Name Value Grade v0 v1
---------- ----------- ----- ----------- -----------
Albert 33 A 0 100
Bob 133 B 100 200
Carl 233 C 200 300
A simple join should get you what you need. The on condition is a little more complicated than normal, but not much.
select t1.DateField, t1.Hour, t2.t2value1, t2.t2value2
from table1 t1
inner join table2 t2 on t2.RangeValue1 <= t1.Value and t1.Value <= t2.RangeValue2
I have really basic knowledge in SQL and this seems like a task that requires a complex query to avoid doing multiple queries in a program loop.
I have a table such as the following with 'filename' as UNIQUE column:
title subtitle filename comment selected
abc 123 f1.txt abc 0
xyz 999 f2.txt bla 0
abc 123 f3.txt ppp 0
poc 232 f4.txt ppp 0
xyz 220 f5.txt ppp 0
xyz 999 f6.txt ppp 0
I need to update the 'selected' column to 1 for only a single row (doesn't matter which) for each unique (title, subtitle) pair. This is how the table should be after the query is processed:
abc 123 f1.txt abc 1
xyz 999 f2.txt bla 1
abc 123 f3.txt ppp 0
poc 232 f4.txt ppp 1
xyz 220 f5.txt ppp 1
xyz 999 f6.txt ppp 0
What is the best way to achieve this?
First, get the IDs of those unique rows:
SELECT MIN(rowid) -- or whatever your primary key is
FROM MyTable
GROUP BY title, subtitle
(It does not matter whether you use MIN or MAX.)
Then update those rows:
UPDATE MyTable
SET selected = 1
WHERE rowid IN (SELECT MIN(rowid)
FROM MyTable
GROUP BY title, subtitle)
In SQLite, I think you can do this:
update table
set selected = 1
where not exists (select 1
from table t2
where t2.name < t.name or (t2.name = t.name and t2.title < t.title)
);
I have tried to find this on SO.
I have a table,
id | col2 | col3
---- ---- ----
5 smith (null)
5 smith 100
12 Jackson 356
12 Jackson 400
8 Johnson (null)
9 bob 1200
In this scenario I only want the rows from a set where the same id only has one non-null. In other words, I don't want smith, I don't want Johnson. I only want Jackson and bob.
I have tried,
select * from table
where is not null a
nd not exists (select * from table where is null)
I can't get it to work.
Your statement and desired results don't quite match, but this will give you every ID that does not have a NULL value in column 3:
SELECT * FROM table
WHERE id NOT IN
(SELECT ID FROM table WHERE col3 IS NULL)
If you want records with just one non-null (which you state but your expected result doesn't match, use
SELECT * FROM table
WHERE id NOT IN
(SELECT ID
FROM table
WHERE col3 IS NOT NULL
GROUP BY ID
HAVING COUNT(id) = 1
)
You can use NOT EXISTS but include a WHERE to reference each id:
select *
from yourtable t
where col3 is not null
and not exists (select id
from yourtable d
where d.col3 is null
and t.id = d.id);
See Demo
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
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