I have around 88k records in another table like ac. I want to update one column of the table ac into main table like tbl.
Eg- Table tbl sample records like
col1 col2 col3 col4
abc dhj 123 ab12
def bhv 456 ds34
ghi hwj 789 hj46
jkl yuh 012 ke28
In table ac sample records like
col1 col3`
cba 123
fed 456
ihg 789
lkj 012
How to update the tbl value from ac table. So that the record look like
col1 col2 col3 col4
cba dhj 123 ab12
fed bhv 456 ds34
ihg hwj 789 hj46
lkj yuh 012 ke28
You can either do a correlated update:
update tbl
set col1 = (select col1 from ac where ac.col3 = tbl.col3)
where exists (select col1 from ac where ac.col3 = tbl.col3);
3 rows updated.
select * from tbl;
COL1 COL2 COL3 COL4
---- ---- ---- ----
cba dhj 123 ab12
fed bhv 456 ds34
ihg hwj 789 hj46
jkl yuh 012 ke28
Or a merge:
merge into tbl
using ac
on (ac.col3 = tbl.col3)
when matched then update set tbl.col1 = ac.col1;
3 rows merged.
select * from tbl;
COL1 COL2 COL3 COL4
---- ---- ---- ----
cba dhj 123 ab12
fed bhv 456 ds34
ihg hwj 789 hj46
jkl yuh 012 ke28
In both cases the fourth row isn't affected as there is no matching record in ac, as #Littlefoot pointed out. The merge just doesn't find a match; for the update version the where exists clause prevents values being set to null if there is no matchign row to update from. If the fourth row in ac was 012 instead of 210 they would all be updated, in both versions.
Related
For some reason, I do not get a result on the SQL Server Freetext search term with the following code.
Both tables are fulltext indexed (in a catalog), and the query somehow works, but as mentioned, I don't get a result as wished...
SELECT [Col8], [Col3]
FROM [Table1]
LEFT JOIN [Table2] ON FREETEXT ([Table1].[Col8] , '[Table2].[Col3]')
Table 1:
ID
Col7
Col8
Col9
1
123
123
123
2
456
456
456
3
789
789
789
4
0
anyText
anyText
Table 2
Col1
Col2
Col3
Col4
1
123
123front
123behind
2
123
middle123middle
middle123middle
3
456
456
456
4
456
midle456
5
789
middle789middle
middle7889
Result:
Col8
Col3
123
NULL
456
NULL
789
NULL
anyText
NULL
I want to find any value in Table2 which matches a value from Table 1, e.g., when I search for "123" (Col8 in Table1), then I would like to get as result (from Col3 in Table2):
123front and
middle123middle
select *
from [Table1], [Table2]
where [Table2].[Col3] LIKE '%' + [Table1].[Col8] + '%'
I like to find duplicate entrys based on the same "Article" AND "Warehouse" column. I cant find a solution for an MSSQL-Query to find out the different "Value1" and "Value2" based on the following table:
Article Value1 Value2 Warehouse
123 123 01.01.2021 1
123 456 02.12.2022 1
123 789 05.05.2024 1
123 123 01.01.2021 2
123 123 01.01.2021 3
456 123 01.01.2021 1
456 123 01.01.2021 1
456 123 01.01.2021 1
The result should be:
Article Value1 Value2 Warehouse
123 123 01.01.2021 1
123 456 02.12.2022 1
123 789 05.05.2024 1
EDIT: The Warehouse and Article is always different. In the result I want to the see the same article and warehouse which has different entry's on value1 and value2.
As you can see the article "123" AND Warehouse "1" has different entry´s on the value1 and value2. So I´d like to get them in the result of the SQL-Query.
But the article "456" has the SAME entry's on value1 and value2 for Warehouse 1, so I don´t wan´t them in the result.
Thank you very much for your help!
Use COUNT DISTINCT.
select *
from mytable t1
where exists
(
select null
from mytable t2
where t2.article = t1.article and t2.warehouse = t1.warehouse
having count(distinct value1) > 1 or count(distinct value2) > 1
)
order by article, warehouse, value1, value2;
(This would be more readable with an IN clause in my opinion, but SQL Server doesn't allow IN clauses on tuples like WHERE (article, warehouse) IN (...).)
Use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.article = t.article and
t2.warehouse = t.warehouse and
(t2.value1 <> t.value1 or t2.value2 <> t.value2)
);
It is unclear from your question whether both values have to be different or either value. The above implements either value being different.
For performance, I would recommend an index on (article, warehouse, value1, value2).
I have a table (tableA):
Col1
Col2
abc
123
def
456
ghi
789
to which I want to add Col3 with entries S, M, L such that (tableB)
Col1
Col2
Col3
abc
123
S
abc
123
M
abc
123
L
def
456
S
def
456
M
def
456
L
ghi
789
S
ghi
789
M
ghi
789
L
I only know of a way to add a column with single values (i.e. by using the ALTER command with default) but not with a column multiple data points.
Add the column
ALTER TABLE tablea
ADD COLUMN col3 varchar(1);
and set the value for the existing rows to 'S'.
UPDATE tablea
SET col3 = 'S';
Then use an INSERT ... SELECT ... from the table cross joined with 'M's and 'L's into the the table to insert the missing rows.
INSERT INTO table3
(col1,
col2,
col3)
SELECT t.col1,
t.col2,
v.col3
FROM tablea t
CROSS JOIN (VALUES ('M'),
('L')) v
(col3);
It was a bit difficult to describe my requirements based on the title, however I'll post with a table sample and result expectation.
I have a table (lets call it TBL_K) that looks like this:
KEY1 KEY2 VALUE1 VALUE2
abc 123 NULL NULL
abc 123 9999 1111
abc 123 9999 1111
ghd 123 NULL NULL
ghd 123 NULL NULL
tiy 134 4444 NULL
tiy 134 4444 NULL
hhh 981 NULL NULL
I want my Select statement to return the result in:
KEY1 KEY2 VALUE1 VALUE2
abc 123 9999 1111
ghd 123 NULL NULL
tiy 134 4444 NULL
hhh 981 NULL NULL
I have came up with own solution with creating two sub-tables with a left outer join but I want to see if there are other ways of creating this result.
It seems nearly to use max() :
select key1, key2, max(val1), max(val2)
from TBL_K tk
group by key1, key2;
SELECT
A.KEY1,
A.KEY2,
B.VALUE1,
B.VALUE2
FROM
(
SELECT
Z.KEY1,
Z.KEY2,
TRIM(Z.VALUE1) VALUE1,
TRIM(Z.VALUE2) VALUE2
FROM
TBL_K Z
WHERE
TRIM(Z.VALUE1) IS NULL
GROUP BY
Z.KEY1,
Z.KEY2,
Z.VALUE1,Z.VALUE2) A LEFT OUTER JOIN
(
SELECT
Y.KEY1,
Y.KEY2,
TRIM(Y.VALUE1) VALUE1,
TRIM(Y.VALUE2) VALUE2
FROM
TBL_K Y
WHERE
TRIM(Y.VALUE1) IS NOT NULL
GROUP BY
Y.KEY1,
Y.KEY2,
Y.VALUE1,Y.VALUE2) B
ON
(A.KEY1 = B.KEY1
AND A.KEY2 = B.KEY2)
I have 4 tables:
table1
ID PNTID col3 col4 col5
123 456 y y 0
444 456 y y 0
900 878 n n 1
table 2
ID TID col3 col4 col5 col6
123 999 777 888 0 x
456 111 988 - - -
444 123 988 - - -
table 2- after update
ID TID col3 col4 col5 col6
123 111 777 888 0 x
456 111 988 - - -
444 111 988 - - -
table 3 -update or insert
TID col2 col3 col4 col5
111 988 x x x
table 4 -update or insert
TID col2 col3 col4 col5
111 988 x x x
I am trying to achieve:
-> Check the col3,col4,col5 if there values matching like Y,Y and 0 then get the values of ID from table 2 that matches PNTID of table1 such as TID , col3..
->Update table 2 wherein ID has same PNTID table1 such as ID : 123, 444
->In table 3,Check TID is there or not if its there then update col2 with the value of table2.col3 of PNTID(ID- col in table 2) else insert a new row as columns in table 2(TID,col,col2,col3...)
Similarly , Need to update or insert table 4 with respect to table 2.
I am trying to build the subqueries .like to get the matched rows of table 1 and 2 the proceed further.
SELECT *
FROM dbo.table tb1
INNER JOIN table2 tb2
ON tb1.ID = tb2.ID
WHERE tb1.col3 = 'Y'
AND tb1.col4 = 'Y'
AND tb1.col5 = 0
this gives me matched rows but how to fetch values and insert into other tables record by record as table 1 and 2 have many records.
can anyone help me on this?
Thanks!
UPDATE <table to update t1>INNER JOIN <table with inner join t2>
ON t1 = t2
SET t1.id =t2.id
WHERE <condition>
the above code is for updation on fly
if this does not work try using triggers