I have 3 tables. They all represent a different stage of a process.
Table1:
ID Process1
1 a
2 b
3 c
4 d
Table 2:
ID ID2 Process2
1 50 e
2 51 f
4 52 g
Table 3:
IDfromPrevious ID3 Process3
50 500 h
51 501 i
52 502 j
3 503 k
I want to create Table 4:
ID3 Process1 Process2 Process3
500 a e h
501 b f i
502 d g j
503 c k
The problem is some item move from process 1 to 3. Other items move from process 1 to 2 to 3. Everything however ends up at Process 3. The item also get assigned a new ID through each process. I am work in ms access 2016.
I don't know where to start with the SQL statement. I've never written if statements in SQL but I want the logic to work something like this:
SELECT Table3.ID3, Table1.Process1, Table2.Process2, Table3.Process3
FROM (IF Table1.ID = Table3.IDfromPrevious Then Table1 INNER JOIN Table3 ON Table1.ID = Table3.IDfromPervious ELSE Table2.ID2 = Table3.IDfromPrevious Then Table2 INNER JOIN Table3 ON Table2.ID2 = Table3.IDfromPervious)
DECLARE #Table1 TABLE
(
ID INT
,Process1 VARCHAR(10)
)
INSERT INTO #Table1
VALUES
(1,'a')
,(2,'b')
,(3,'c')
,(4,'d')
DECLARE #Table2 TABLE
(
ID INT
,ID2 INT
,Process2 VARCHAR(10)
)
INSERT INTO #Table2
VALUES
(1,50,'e')
,(2,51,'f')
,(4,52,'g')
DECLARE #Table3 TABLE
(
IDfromPrevious INT
,ID3 INT
,Process3 VARCHAR(10)
)
INSERT INTO #Table3
VALUES
(50,500,'h')
,(51,501,'i')
,(52,502,'j')
,(3,503,'k')
SELECT * FROM #Table1
SELECT * FROM #Table2
SELECT * FROM #Table3
SELECT
ID3
,Process1
,Process2
,Process3
FROM #Table1 T1
LEFT JOIN #Table2 T2 ON T1.ID = T2.ID
LEFT JOIN #Table3 T3 ON T3.IDfromPrevious IN (T2.ID2,T1.ID)
do it the simple way, join both conditions and then keep the not NULL
select t3.ID3, isnull(t1.process1, tx.Process1) Process1, t2.Process2, t3.Process3
from t3
left join t2 on t3.IDfromPrevious = t2.ID2
left join t1 on t2.ID = t1.ID
left join t1 tx on t3.IDfromPrevious = tx.ID
MS Access is only supports a subset of standard ISO SQL. After several attempts it seems this is only possible via a subquery. That subquery can then be outer joined with an Nz condition as follows:
SELECT Table3.ID3, Table12.Process1, Table12.Process2, Table3.Process3
FROM (SELECT Table2.ID2, Table1.ID, Table1.Process1, Table2.Process2
FROM Table1
LEFT JOIN Table2 ON Table2.ID = Table1.ID) AS Table12
LEFT JOIN Table3 ON Table3.IDfromPrevious = Nz(Table12.ID2, Table12.ID)
Related
currently I have 3 tables
Table 1:
tb1_id
values
1
4
Table 2:
tb1_id
tbl3_id
1
5
Table 3:
tb3_id
values
5
2
For some reason the values from table 1 are not the same in table 3 (as shown above), I need to update the values of the table 1 with the table 3, but I am not able to do so, so far this is my query:
UPDATE
table1 t1
SET
values = temp_tbl.values
FROM
(
SELECT t2.tb1_id, t3.values FROM table2 t2
JOIN
table3 t3 ON t2.tbl3_id = t3.tbl3_id
) temp_tbl
WHERE
t1.tbl1_id = temp_tbl.tbl1_id
AND
t1.values != temp_tbl.values;
OK so if I understood you correctly, you just need to make a slight correction to your code:
UPDATE
table1 t1
SET
values = temp_tbl.values
FROM
(
SELECT t2.tbl1_id, t3.values
FROM table2 t2
Inner JOIN table3 t3 ON t2.tbl3_id = t3.tbl3_id
) temp_tbl
WHERE
t1.tbl1_id = temp_tbl.tbl1_id
AND
t1.values != temp_tbl.values;
Here is DBFiddle link.
EDIT: This would also do:
with newData as (
select t1.tbl1_id, t3.Values
from Table1 t1
inner join Table2 t2 on t1.Tbl1_Id = t2.Tbl1_Id
inner join Table3 t3 on t2.Tbl3_Id = t3.Tbl3_Id
where t1.Values != t3.Values
)
UPDATE
table1
SET
values = newData.Values
FROM newData
where table1.tbl1_id = newData.tbl1_id;
this is how you can do it:
-- temporary tables
SELECT 1 TBL1_ID, 4 valuet1 INTO #t1
SELECT 1 TBL1_ID, 5 TBL3_Id INTO #t2
SELECT 5 TBL3_ID, 2 valuet3 INTO #t3
-- select with joins
select T1.*,T2.*, T3.*
from #t1 T1
INNER JOIN #t2 T2
ON T1.TBL1_ID = T2.TBL1_ID
INNER JOIN #t3 T3
ON T3.TBL3_ID = T2.TBL3_ID
-- update would be like this
update #t1 set valuet1 = T3.valuet3
from #t1 T1
INNER JOIN #t2 T2
ON T1.TBL1_ID = T2.TBL1_ID
INNER JOIN #t3 T3
ON T3.TBL3_ID = T2.TBL3_ID
I currently have three tables as below:
table 1
book_id margin
-------------------
b1 10
b2 20
b3 30
table 2
t2_id book_id author_id
-----------------------------
1 b1 100
2 b2 200
3 b3 300
table 3
author_id revenue
----------------------
100 0
200 0
300 0
I am trying to update revenue on table3 with 50% of the margin from table1 for the corresponding author (on table3) of the book. The outcome should update the table 3 to:
author_id revenue
----------------------
100 10
200 20
300 30
I can update values from another table if they are directly linked together with a common key, I am struggling having to reference another table in the middle to get to the answer :(
I have tried:
UPDATE table3 t3 SET revenue =
(SELECT t1.margin FROM table1 t1 WHERE
(SELECT t1.book_id FROM table1 t1 JOIN table2 t2 ON t1.book_id = t2.book_id) =
(SELECT author_id FROM table3 t3 JOIN table2 t2 ON t3.authoer_id = t2.author_id));
Thanks
Use update with join
update
(
SELECT table3.revenue as OLD, table1.margin as NEW
FROM table1 INNER JOIN table2 on table1.book_id=table2.book_id
inner join table3 on table2.author_id=table3.author_id
)t set t.old=t.new
use merge update with a subquery
MERGE INTO table3 t3
using
(select t1.margin,t2.author_id
from tabl1 t1 join table2 t2 on t1.book_id=t2.book_id
) a ON (t3.author_id = a.author_id)
when matchced then
update SET t3.revenue = a.margin
You can use update by using with..as method also :
update table3 t3
set t3.revenue =
(with t as (
select *
from table1 t1
join table2 t2 on t2.book_id = t1.book_id
)
select t.margin from t where t.author_id = t3.author_id);
Demo
I have this scenario question which needs to be solved using joins only. can't use except,intersect or union.
sample code:
--demo setup
create table t1 (id int)
insert into t1 values (1),(2),(3)
create table t2 (id int)
insert into t2 values (4),(5),(6)
--join
select t1.id,t2.id
from t1 full outer join
t2 on t1.id=t2.id
--after join i am getting
id id
----------- -----------
1 NULL
2 NULL
3 NULL
NULL 4
NULL 5
NULL 6
--But i need is
id
-----------
1
2
3
4
5
6
Can someone help me with this ? i know this can be easily done using union but this challenge needs to be solved using joins only.
any help is appreciated ....
--solved the challenge after adding case to the code.
select case when t1.id is null then t2.id else t1.id end as id
from t1 full outer join
t2 on t1.id=t2.id
you can use this code:
SELECT case when t1.id IS NULL THEN t2.id ELSE t1.id END AS id
FROM t1 FULL OUTER JOIN t2
ON t1.id=t2.id
As you said the Use From Union
select * from t1
union
select * from t2
Although UNION ALL is the better option, you can use this following script-
SELECT
CASE WHEN C.ID1 IS NULL THEN C.ID2 ELSE C.ID1 END V
FROM
(
SELECT * FROM
(
SELECT 'T1' TabName1,id ID1 FROM #t1
)A
FULL JOIN (
SELECT 'T2' TabName2,id ID2 FROM #t2
)B
ON A.TabName1 = B.TabName2
)C
ORDER BY 1
I have two tables like the following:
Table1
Id Table1_Col
1 A
2 B
3 C
4 D
5 E
Table2
Id Table1_Col Table2_Col
1 A Test
I want the count of (Table1_Col) in Table2 and I need query for the following output:
Expected Output
Table1_Col Count_Table2_Col
A 1
B 0
C 0
D 0
E 0
What I have tried so far:
select Table1_Col,Count(Table2_Col) from table1 t1
Left outer join table2 t2 on t1.Table1_Col = t2.Table1_Col
Please provide me a proper solution for this.
You need GROUP BY, when using aggregate methods. Also Table1_Col existing in both tables, so please use with the proper table alias for the columns.
The query below will return your expected result. Please find the demo too.
select T1.Table1_Col, Count(T2.Table2_Col) AS Table2_Col
from table1 t1
Left outer join table2 t2 on t1.Table1_Col = t2.Table1_Col
GROUP BY T1.Table1_Col
Demo on db<>fiddle
UPDATE: As per the comment in the post, based on your fiddle, the condition t3.visitno=1 should be in the LEFT OUTER JOIN and not in the WHERE clause, so the following query will work:
select t3.pvisitno, t1.DocName, count(t2.vdocid) as [count]
from Document_type t1
left outer join visitdocs t2 on t2.DocId = t1.DocId
left outer join visittbl t3 on t3.visitno = t2.visitno and t3.visitno=1
group by t3.pvisitno,t1.DocName
order by count(t2.vdocid) desc
db<>fiddle demo for the revised fiddle
Try this query:
select t1.Table1_Col,
sum(case when Table2_Col is null then 0 else 1 end) Count_Table2_Col
from Table1_Col t1
left join Table2 t2 on t1.Table1_Col = t2.Table1_Col
group by t1.Table1_Col
You can try this:
Declare #t table ( id int ,col varchar(50))
insert into #t values (1,'A')
insert into #t values (2,'B')
insert into #t values (3,'C')
Declare #t1 table ( id int ,col varchar(50),col2 varchar(50))
insert into #t1 values (1,'A','TEST')
select t.col,count(t1.id) countT2 from #t t left join #t1 t1
on t.id=t1.id
group by t.col
Here's another option:
select t1.Table1_Col, coalesce(x.cnt, 0) cnt
from table1 t1
left outer join (select Table2_Col, count(*) cnt from table2 group by Table2_Col) x
on x.Table2_Col = t1.Table1_Col;
The idea here is to create an inline view of table2 with its counts and then left join that with the original table.
The "coalesce" is necessary because the inline view will only have records for the rows in table2, so any gaps would be "null" in the query, while you specified you want "0".
I have 2 tables.
Table1
Id
1
2
3
4
Table2
VehicleId Value Table1Id
1 t 1
1 q 2
3 w 3
3 e 4
4 t 1
5 e 1
5 f 2
5 g 4
How can I get it so it always returns Id 1-4 of the first table then joins the values if they do exist in table 2.
For Example a result set would be:
VehicleId Value
1 t
1 q
1
1
3 w
3 e
3
3
So I always want all the results returned from table 1 even if there are no values for it in table 2?
Thanks
I don't see how your example output matches the sample data you provided--was it meant to?
But I think all you need is a LEFT JOIN...
SELECT Table2.VehicleId, Table2.Value
FROM Table1
LEFT JOIN Table2 ON Table1.Id = Table2.Table1Id;
The problems are arising because your Table2 is not really a 'vehicle' table. Because the VehicleId does not uniquely identify a record in that table. This is where all of the confusion is coming from. So to solve that and get your problem to work I did a select distinct on table2 against the values in table 1 (I also did a select distinct for clarity, but it was not necessary.) Hope this helps.
CREATE TABLE #Table1 (Id INT)
CREATE TABLE #Table2 (VehicleID INT, Value VARCHAR(50), Table1ID INT)
INSERT INTO #Table1 VALUES (1),(2),(3),(4),(5)
INSERT INTO #Table2 VALUES (1, 't', 1),(1, 'q', 2),(3, 'w', 3),(3, 'e', 4),(4, 't', 1),(5, 'e', 1),(5, 'f', 2),(5, 'g', 4)
SELECT * FROM #Table1
SELECT * FROM #Table2
SELECT t2.VehicleID, t2.Value
FROM ( SELECT t2.VehicleId, t1.Id
FROM ( SELECT DISTINCT
VehicleId
FROM #Table2 ) t2
CROSS JOIN ( SELECT Id
FROM #Table1 ) t1 ) Base
LEFT JOIN #Table2 t2
ON Base.VehicleId = t2.VehicleID
AND Base.Id = t2.Table1ID
WHERE (Base.VehicleId BETWEEN 1 AND 3)
DROP TABLE #Table1
DROP TABLE #Table2
Try this
SELECT TAB1.Id,
TAB2.VehicleId,
TAB2.Value
FROM Table1 TAB1 LEFT JOIN Table2 TAB2 ON TAB1.Id = TAB2.Table1Id