I have 3 tables. Table 1 gets updated every time I upload a file. Table 2 is an archival table that stores records in all files uploaded. Table 3 stores data of table 2 after some validations.
1st File upload:
Table 1:
RefNo | Code | ID
--------+----------+-----
1234 30 1
5678 40 2
Table 2:
RefNo | Code | ID
--------+----------+------
1234 30 1
5678 40 2
2nd File upload
Table 1:
RefNo | Code | ID
--------+----------+-----
1234 50 3
5678 60 4
Table 2:
RefNo | Code | ID
--------+----------+------
1234 30 1
5678 40 2
1234 50 3
5678 60 4
3rd File upload (UPDATE code=100 for refno = 1234)
Table 1:
RefNo | Code | ID
--------+----------+------
1234 100 5
Table 2:
RefNo | Code | ID
--------+----------+------
1234 30 1
5678 40 2
1234 50 3
5678 60 4
1234 100 5
Now, there are 3 records for 1234 in table 2. I want to update code to 100 in table 3 only if last uploaded code is 0. How can I select the last uploaded record?
SELECT *
FROM table2 a
JOIN table 1 b ON a.refno = b.refno
WHERE a.id = (SELECT ???)
I don't know how to pick ID = 3 (last uploaded one) and not multiple IDs.
if the ids are type int, you can use this to get the last record for all refno's in table1 that exist in table2. Then you can filter those as needed:
select t2.*
from table2 t2
join (
select t2.RefNo,
MAX(t2.ID) lastId
from table2 t2
JOIN table1 t1 ON t2.RefNo = t1.RefNo
WHERE t2.Id < t1.Id
GROUP BY t2.RefNo
) ti on t2.Id = ti.lastId
Related
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
I'm having a trouble writing a query in ORACLE. I have a Table that contains values. for example:
ID quantity partID
123 50 10
100 20 10
100 30 11
123 null 8
456 null 100
789 25 123
456 50 9
I want to get all rows that has same ID but quantities to be 50 and null (exact same pairs of 50 and null only). for the given example I would like to get:
ID quantity partID
123 50 10
123 null 8
456 50 9
456 null 100
I tried inner join but it doesn't provide the exact output as expected.
You may try :
select ID, quantity, partID
from tab
where ID in
(
select ID
from tab
where nvl(quantity,50)=50
group by ID
having count(distinct nvl(quantity,0) )>1
);
ID QUANTITY PARTID
123 50 10
123 (null) 8
456 (null) 100
456 50 9
SQL Fiddle Demo
P.S. you may get the same results by commenting out having count(ID)=2 also but for those cases there may not exist one of 50 or null for values of quantity.
You can use exists:
select t.*
from t
where (t.quantity = 50 and
exists (select 1 from t t2 where t2.id = t.id and t2.partid = t.partid and t2.value is null)
) or
(t.quantity is null and
exists (select 1 from t t2 where t2.id = t.id and t2.partid = t.partid and t2.value = 50)
) ;
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 2 tables:
Table1 - Criteria
Office_ID Bus_Stream Bus_Criteria Crit_Value
1 ABC 0 20
1 ABC 1 21
1 ABC 2 7
2 ABC 0 15
2 ABC 1 12
2 ABC 2 21
3 XYZ 1 17
3 XYZ 2 3
Table2 - Limit
Bus_Stream GroupID TypeID SubgroupID Bus_Limit
ABC 20 21 7 50
ABC 15 12 21 100
XYZ 99 17 3 120
I need to create a join that allows me to pull back:
Result
Bus_Stream Office_ID GroupID TypeID SubgroupID Bus_Limit
ABC 1 20 21 7 50
ABC 2 15 12 21 100
XYZ 3 (null) 17 3 120
Essentially, I need to join Table1.Crit_Value based on the following:
Table1.Bus_Criteria Table2
0 GroupID
1 TypeID
2 SubGroupID
with the added complication that if one or two of the 0/1/2 values from Bus_Criteria is missing, the joins will still occur on the remaining criteria.
I have tried a number of combinations of AND/OR on the join to no avail.
Any ideas folks?
This may be what you're after.. use a case statement on the join.
The problem here is your data in t2 isn't normalized, you could also unpivot the 3 data columns in t2 so the join is more natural.
SELECT T2.Bus_Stream, T1.Office_ID, T2.GroupID, T2.TypeID, T2.SubGroupId, T2.bus_Limit
FROM T1
INNER JOIN T2
on T1.Bus_Stream = T2.Bus_Stream
and T1.Crit_value = case when T1.Bus_Critiera = 0 then T2.GroupID
when T1.Bus_Critiera = 1 then T2.TypeID
when T1.Bus_Critiera = 2 then T2.SubGroupID
end
Did you try something like this?
SELECT
t1.*, t2.*
FROM Table1 t1
INNER JOIN Table2 t2 ON
t1.Bus_Stream = t2.Bus_Stream AND
CASE
WHEN t1.Bus_Criteria = 0
THEN t2.GroupID = t1.Crit_Value
WHEN t1.Bus_Criteria = 1
THEN t2.TypeID = t1.Crit_Value
ELSE
t2.SubGroupID = t1.Crit_Value
END
I have Table1:
Id Program Price Age
12345 ABC 10 1
12345 CDE 23 3
12345 FGH 43 2
12346 ABC 5 4
12346 CDE 2 5
12367 CDE 10 6
and a Table2:
ID Program BestBefore
12345 ABC 2
12345 FGH 3
12346 ABC 1
I want to get the following Table,
Id Program Price Age
12345 CDE 10 1
12346 CDE 2 5
12367 CDE 10 6
I.e get the rows from the first table where the ID+Program is not in second table. I am using MS SQL Server express 2012 and I don't want to add any columns to the original databases. Is it possible to do without creating temporary variables?
Several ways to do this, here's one using not exists:
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
One possible variant is to use LEFT JOIN:
SELECT
Table1.*
FROM
Table1
LEFT JOIN Table2
ON Table1.ID = Table2.ID
AND Table1.Program = Table2.Program
WHERE
Table2.ID IS NULL