I have a table with records of the same type with ID's and a grouping. I need to update the id's of table 1 into table 2 based on the record id from largest to smallest using the position.
Table 1
ID
Group
Name
1
A
Apple
2
A
Apple
3
B
Apple
4
B
Apple
Table 2
ID
recordid
position
group
1
1
250
A
2
2
350
A
3
null
450
A
4
null
550
A
5
3
250
B
6
4
350
B
7
null
450
B
8
null
550
B
update table2
set recordid=T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
This isn't distinct so I don't think it's right, but I can't figure it out.
where ?????
the problem is you have multiple Id per group , so you have to choose one :
update table2
set recordid= ( select top 1 T1.ID
From Table1 T1
join Table2 T2 on T2.Group=T1.Group
)
where recordid is null
Related
I've been struggling with this problem.
I have 2 populated tables and a third empty one and I would like to populate the third with data from the other two. Correlate values
The idea is to assign every single ID from the 1st table to entries (several) found in the 2nd table that respect the condition where DestinedUserTypeID should be equal to UserTypeID),
Table 1
Id
UserName
UserTypeID
1
Bla
1
2
Ble
2
3
Bli
3
Table 2
ID
TaskName
DestinedUserTypeID
1
Task A
1
2
Task B
1
3
Task C
1
4
Task D
2
DESIRED TABLE, Table 3
ID
UserID
TaskID
1
1
1
2
1
2
3
1
3
...
Insert into Table3 ([UserId],[TaskID])
SELECT
Id,
(SELECT [Id] FROM [Table2] t2
WHERE [Id] <= 5 AND [DestinedUserTypeId] = 1)
FROM t1 WHERE [UserTypeId] = 1
Thank you!
You can do it with a simple join linked to your table 1 and 2 and just insert into your table3
SELECT
u.id,
t.id,
from
table1 as u inner join
table2 as t on u.userTypeId = t.DestinedUserTypeID
i have to similar tables table 1 and table 2 .I want to compare the 2 tables and show the rows that ID is the same but has a different value in another column.
table 1
ID
ACTIVE
100
1
221
1
341
1
and
table 2
ID
ACTIVE
100
1
221
0
341
1
the output should be like this:
ID
ACTIVE
221
1
select t1.*
from t1
join t2 on t1.id = t2.id
where t1.active <> t2.active
Tabel 1 :
ID1
ID2
ID3
MainID
Location
1
A
X
1AX
VIC
2
B
Y
2BY
SYD
3
C
W
3CW
TAS
4
D
Z
4DZ
TAS
Tabel 2 :
SALESID
QTY
AMT
DIFF
1AX
1
100
2
2BY
2
0
3
3CW
3
5
4DZ
3
12
2
Ignore other fields, I need to delete all raws in Tabel 1 where AMT in Tabel 2 has zero or no value for the SALESID.
For example, after the query, only raws containing 1AX & 4DZ should be remain in Tabel 1.
this can be done by subquery
get all MainID from table2 where amt is 0 or null
delete all rows that equal to previous fetched MainID
delete from table1 where MainID in (
select SALESID from table2 where AMT <=0 or AMT is null
)
You can use exists:
delete from table1
where exists (select 1
from table2 t2
where table1.mainid = t2.salesid and
(t2.amt = 0 or t2.amt is null)
);
Thinking an INNER JOIN would be much faster on larger data sets. Something like this:
DELETE
T1
FROM
Table1 T1
INNER JOIN
Table2 T2 ON T2.SalesID = T1.MainID
WHERE
IsNull(T2.Amt,0) = 0
I have two tables, I want to match values in both tables/pairing fields as below
In table 1 records 1 and 2 should be updated in MatchRef field as matched
In table 2 records 1 and 2 should be updated in matchref field as matched
but in Table 1 the record 3 should not updated as matched as there is no matching field in table 2
Table 1
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
3 AAA 100
4 BBB 100
Table 2
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
4 BBB 100
Here ID is not an unique filed, its just auto number.
How to do it in Ms Access? There will be 600 000 records on average in each table and working on a Citrix environment.
Here is how you would find matching records as far as I can tell --
SELECT
*
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.ID = T2.ID
AND T1.MATCHFIELD = T2.MATCHFIELD
I have a three tables
Table 1
Id Department
1 A
2 B
3 C
4 D
Table 2
Id DepartId Name
1 1 ABC
2 1 DEF
3 1 ASD
4 2 FGH
5 2 HJK
6 3 ZXC
Table 3
Id Depart Area
1 A pp
2 B
3 C nn
4 D oo
I need the result
Id Depart Name Area
1 A ABC pp
2 B FGH Null
3 C ZXC nn
4 D NULL oo
I need one matching entry from table 2 and table 3 to corresponding entry in the table 1
Do a left join to also get t1 rows without any reference in the t2 table. GROUP BY to get only 1 row per Department.
select t1.id, t1.Department, min(t2.Name)
from t1
left join t2 on t1.id = t2.DepartId
group by t1.id, t1.Department
I think I would do this with a correlated subquery:
select t1.*,
(select t2.name
from t2
where t1.id = t2.DepartId and rownum = 1
) as t2name
from t1;
This saves the overhead of an aggregation. An index on t2(DepartId, name) is optimal for this query.
by the way not the answer to your specific question but if instead of just one you want all the names you can use listagg
SELECT t1.id,
department,
LISTAGG (name, ',') WITHIN GROUP (ORDER BY name) names
FROM t1, t2
WHERE t1.id = t2.departId(+)
GROUP BY t1.id, department
ORDER BY 1
ID Department Names
1 A ABC,ASD,DEF
2 B FGH, HJK
3 C ZXC
4 D