Update access table with values from another table using VBA - ms-access

I have two tables, table 1 and table 2, in a database.
I am trying to update table 1 using VBA code based on data in table 2.
Example:
Table 1
PartNo Price Description
--------------------------
A 100
B 200 Bad
C 300
Table 2
PartNo Price Description
--------------------------
A 700
B 200 Good
D 900 Used
After the update, table1 should be updated with those prices or descriptions that have changed where table1 PartNo = table 2 PartNo , and add any new items that were present in table 2.
Table 1
PartNo Price Description
--------------------------
A 700
B 200 Good
C 300
D 900 Used
I can't seem to get it quite right, appreciate the help.

You can do it with two statements, an update and an insert like this:
Update:
UPDATE Table1
INNER JOIN table2
ON(table1.partNo = table2.PartNo)
SET table1.price = table2.price,
table1.description = table2.description
And then an insert:
INSERT INTO table1 (PartNo,Price,Description)
SELECT PartNo,Price,Description FROM table2 t
WHERE NOT EXISTS(SELECT 1 FROM table1 s
WHERE t.PartNo = s.PartNo)

Related

Get count of a column based on join from other table

I am new to SQL. I have to tables and trying to get the count of column from table 1 and join by other column in table 2.
Table 1:
credits | sec_code | student_acc_id
--------------------------------
4 TUB 2098
5 JIY 2099
6 THG 3011
Table 2:
id| sec_code | student_acc_id | stu_id
-------------------------------------
1 TUB 2098 1011
5 JIY 2099 1011
7 THG 3011 1012
I would like to get the sum of credits for the student by getting the stu_id from table2 from sec_code and get all the student_acc_id for stuId and sum the credits column in table1 for all the student account Ids found from table 2. I am not sure how can we join or make this query simple.
Normally my approach is to inlcude this two to three different SQL statements, but i am looking for this in a one sql query if possible.
For the above example lets say i want to get sum of credits for all student_acc_id where stu_id is 1011 from second table. i just have the first table. So the output should be 4+5 as both accounts belong to the same student.
So i need:
--> get the stu_id based on sec_code from table two (lets say for TUB sec_code)
--> get all student_acc_id from table where stu_id is result from above statement
-->now using those all student_acc_id's sum the credit in table 1
Any help is appreciated !
Use Join according to your Data exist.
But as per the sample data better to get Data from Right table.
select
tab2.stud_id,
SUM(tab1.credits) AS credit_sum
from
table1 as tab1
right join
table2 as tab2
on tab1.student_acc_id = tab2.student_acc_id
and tab1.sec_cd = tab2.sec_cd
where
tab2.stud_id in(
select distinct stud_id
from table2
where student_acc_id = '2098'
)
group by
tab2.stud_id;
You can check its sample output here. Please click for fiddle solution.
try like below
select t2.stu_id,
sum(t1.credits)
from t1 join t2 on t1.student_acc_id=t2.student_acc_id and
t1.sec_code=t2.sec_code
group by stu_id
Having count(stu_id)>1

How can I retrieve multiple rows from a table and display the combined information in a single query?

I have used a dynamic form builder plugin in WordPress. When I input some data from this form data inserted in the table. The table looks like below.
id entry_id field_id slug value
1 1 fld_7213434 name abc
2 1 fld_5474822 father def
3 1 fld_4459896 gender Female
4 1 fld_6364274 village_name ijk
20 2 fld_7213434 name J Jahan
21 2 fld_5474822 father Md Ali
22 2 fld_4459896 gender Female
23 2 fld_6364274 village_name ijk
I need to show this table's data like below
S.N. name father gender village_name
1 abc def Female ijk
2 J Jahan Md Ali Female Adabor
How can I do that?
One idea is to join the table to itself:
SELECT
t1.entry_id `S.N.`,
t1.value name,
t2. value father,
t3.value gender,
t4.value village_name
FROM mytable t1
LEFT JOIN mytable t2 ON t2.entry_id = t1.entry_id AND t2.field_id = 'fld_5474822'
LEFT JOIN mytable t3 ON t3.entry_id = t1.entry_id AND t3.field_id = 'fld_4459896'
LEFT JOIN mytable t4 ON t4.entry_id = t1.entry_id AND t4.field_id = 'fld_6364274'
WHERE
t1.field_id = 'fld_7213434'
Of course, instead of mytable you have to use your correct table name that you have not mentioned in your question.
A join is the Cartesion product (Wikipedia) of all involved tables. After filtering the resulting set of rows for those with the correct combinations (in this case, entry_id and field_id must be filtered), you get exactly what you want.
For performance reasons, you might want to have an index on the columns entry_id and field_id, but you'll find out.

Sql: How to connect tables by just using the ID

so I have this problem: I have 3 different tables that need to be connected to one another. It's rather complex and my english skills aren't that good. Let me show you:
T1 T2 T3
ID_T1 |Name | |ID_T1|ID_T2| |ID_T2|Spent|
1 |James| | 1| 4| | 4 | 200 |
2 |Mike | | 2| 5| | 5 | 300 |
3 |Alex | | 3| 6| | 6 | 400 |
Basically I want to connect in Table T3 the amount spent with the names in T1 by using the ID I have in T2.
So I should "read" that James spent 200 dollars, Mike spent 300 and Alex 400.
Another thing to know is that the IDs are generated automatically, so I'm not supposed to be able to see them.
Do I have to pay attention on something when I create my Tables, or do I have to focus more on the INSERT INTO command (I work with Oracle sql Developer)?
Thank you very much! :)
#Burkinaboy might wanna try this
SELECT
T1.Name,
T3.SPent
FROM T1 t0 RIGHT JOIN T2 t1 ON T1.ID_T1 = T2.ID_T1 RIGHT JOIN T3 t2 ON T3.ID_T2=T2.ID_T2
This should give you an expected output.
Select a.Name, c.spend
From T1 a, T2 b, T3 c
Where a.Id_T1 = b.Id_T1
AND b.Id_T2 = c.Id_T2
Regarding your second question, you should know the relation between the tables meaning how they are connected.
Updating the answer as per your comments . Assuming that amount spend column you are going to hard code..
Select b.Id_T2 , "300"
From T1 a ,T2 b
Where a.Id_T1 = b.Id_T1
But in above case , all the values for column spend would be 300.
SELECT T1.name, T3.spent FROM T1, T3 WHERE T1.ID_T1=T2.ID_T1 AND T2.ID_T2=T3.ID_T2
select t1.name as name, t3.spent as spent
from t1
join t2 on t2.id_t1 = t1.id_t1
join t3 on t3.id_t2 = t2.id_t2
Use join instead of putting all tables in FROM clause.
Assuming you are inserting every spending in T3 table with a new ID, you can join the three tables and do a group by on it:
select
T1.ID_T1 ID,
T1.NAME,
SUM(T3.SPENT) SPENT
from T1 inner join T2 on T1.ID_T1 = T2.ID_T1
inner join T3 on T2.ID_T2 = T3.ID_T2
GROUP BY T1.ID_T1, T1.NAME;
As of now, It's not clear which database you're using exactly. (I assumed that it's Oracle as you're using Oracle SQL Developer. Similar solution should be available in other DBMS as well in case it's not.).
First, Let's create the tables:
create table t1 (
ID_T1 integer primary key,
Name varchar2(100)
);
create table t2 (
ID_T1 integer,
ID_T2 integer,
constraint pk primary key(ID_T1,ID_T2)
);
create table t3 (
ID_T2 integer primary key,
spent number(10,2)
);
Then create two sequences that'll be used while inserting:
create sequence t1_seq start with 1 increment by 1;
create sequence t2_seq start with 1 increment by 1;
Then a view which will display spendings per person:
create or replace view spendings
as
select
T1.ID_T1 ID,
T1.NAME,
SUM(T3.SPENT) SPENT
from T1 inner join T2 on T1.ID_T1 = T2.ID_T1
inner join T3 on T2.ID_T2 = T3.ID_T2
GROUP BY T1.ID_T1, T1.NAME;
Then create an INSTEAD OF trigger on the above view to make the proper inserts in underlying tables:
create or replace trigger spendings_trig
instead of INSERT or UPDATE or DELETE on SPENDINGS
for each row
declare
v_ID_T1 integer;
v_ID_T2 integer;
v_name t1.name%type;
begin
if not inserting then
raise_application_error(-20001,'Not supported');
end if;
begin
if :new.ID is null then
v_ID_T1 := T1_SEQ.NEXTVAL;
insert into T1 (ID_T1, NAME) values (v_ID_T1, :new.NAME);
end if;
select ID_T1,NAME into v_ID_T1,v_name from T1
where ID_T1 = :new.ID;
if :new.name is not null and v_name <> :new.name then
raise_application_error(-20002,'Incorrect name entered');
end if;
exception
when no_data_found then
v_ID_T1 := :new.ID;
insert into T1 (ID_T1, NAME) values (v_ID_T1, :new.NAME);
end;
v_ID_T2 := T2_SEQ.NEXTVAL;
insert into T2 values (v_ID_T1, v_ID_T2);
insert into T3 values (v_ID_T2, :new.spent);
end;
/
Now if you insert into the view like this (assuming there is no data in any of the tables):
insert into spendings (name, spent) values ('James',100);
T1 will get a row
ID_T1 NAME
------------
1 James
T2 will get a row
ID_T1 ID_T2
------------
1 1
T3 will get a row
ID_T2 SPENT
-------------
1 100
Now if you do:
insert into spendings (id,spent) values (1,100);
T1 will not be inserted as there is already row present for ID = 1
T2 will get a row:
ID_T1 ID_T2
------------
1 1
1 2
T3 will get a row
ID_T2 SPENT
-------------
1 100
2 100
And
select * from spendings;
will show:
ID NAME SPENT
------------------
1 James 200

MySQL 5.1.61 - Update table based on two seperate tables

I have three tables:
Table_1:
id name
1 NULL
2 OLED
3 legion
4 project100
5 group3
6 0
7 25
Table_2:
projectID externalID projectTypeID projectDescription
0 0 5 UNALLOCATED
25 220339 1 OLED
Table_3:
typeID typeDesc
1 Playbook Aligned
2 Transactional Project
3 External Programs
4 UPI
5 Unallocated
I am trying to update Table_1. I only want to update rows with a 'name' that is a digit. I know that I can select those by doing:
SELECT `name`
FROM `Table_1`
WHERE `name` REGEXP '^[0-9]*$'
This gives me:
name
0
25
What I want to do now is to update these Table_1 entries based on Table_2 and Table_3. I need to find the row in Table_2 where Table_2.projectID = Table_1.name. Then, I need to find the row in Table_3 where Table_3.typeID = Table_2.projectTypeID. Finally, I need to update Table_1.name with Table_3.typeDesc. It's a confusing situation - unfortunately I can't do much to change the way that these tables are set up. Any help is appreciated.
UPDATE
Table_1
JOIN Table_2 on(Table_2.projectID = Table_1.name)
JOIN Table_3 on(Table_3.typeID = Table_2.projectTypeID)
SET Table_1.name = Table_3.typeDesc
WHERE Table_1.name REGEXP '^[0-9]*$';

SQL joining 2 tables with some overlapping values

I am trying to create one table out of the following 2 tables and running into a lot of problems.
Table 1 has the following:
Month,
Salesperson_Assigned,
Product1_Revenue
Table 2 has the following (different product):
Month,
Salesperson_Assigned,
Product2_Revenue
The problem is that while all the months are the same, there are some salespeople in Table 1 not in Table 2, and vice versa. Obviously the revenue is different. I want to join the tables to look like Month, Salesperson, Product1_revenue + Product2_revenue.
Here's the query I'm using:
SELECT
Table1.month,
Table1.salesperson_assigned,
sum(Table1.revenue + Table2.revenue)
FROM
Table1
INNER JOIN Table 2 ON (Table1.month = Table2.month) AND (Table1.salesperson_assigned = Table2.salesperson_assigned)
The output isn't correct even though I confirmed the revenue values in each individual table are correct. The output seems to be making up additional values for the salespeople who appear in only 1 table for the field that should be null.
Anyone had any advice for how to join the 2 tables properly so it acts similar to a pivot table, adding the unique values to the "salesperson assigned" column and adding the revenue for both columns, but when a salesperson is in only ONE table having it respect that there is a zero value for revenue?
did you tried this way:
SELECT
Table1.month,
Table1.salesperson_assigned,
sum(Table1.revenue + Table2.revenue)
FROM
Table1 FULL OUTER JOIN Table2 on (Table1.month = Table2.month AND Table1.salesperson_assigned = Table2.salesperson_assigned)
Ideally for these type of situations FULL OUTER JOINS are useful - but apparently MySQL does not support FULL OUTER JOINS see: Full Outer Join in MySQL
Update (without using right outer join):
create table table1
(month int
,salesperson_assigned int
,Product1_revenue int);
create table table2
(month int
,salesperson_assigned int
,Product2_revenue int);
insert into table1 values(1,10,100);
insert into table1 values(2,10,200);
insert into table1 values(1,11,40);
insert into table1 values(2,11,800);
insert into table1 values(3,11,400);
insert into table2 values(1,10,100);
insert into table2 values(2,10,200);
insert into table2 values(1,12,40);
insert into table2 values(2,12,200);
select
table1.month
,table1.salesperson_assigned
,ifnull(table1.Product1_revenue,0) as Product1_revenue
,ifnull(table2.Product2_revenue,0) as Product2_revenue
,ifnull(table1.Product1_revenue,0)+ifnull(table2.Product2_revenue,0) as total_revenue
from table1
left outer join table2
on table1.salesperson_assigned=table2.salesperson_assigned and table1.month=table2.month
union
select
table2.month
,table2.salesperson_assigned
,ifnull(table1.Product1_revenue,0) as Product1_revenue
,ifnull(table2.Product2_revenue,0) as Product2_revenue
,ifnull(table1.Product1_revenue,0)+ifnull(table2.Product2_revenue,0) as total_revenue
from table2
left outer join table1
on table2.salesperson_assigned=table1.salesperson_assigned and table1.month=table2.month
order by 2,1;
returns:
Month salesperson_assigned Product1_revenue Product2_revenue total_revenue
1 10 100 100 200
2 10 200 200 400
1 11 40 0 40
2 11 800 0 800
3 11 400 0 400
1 12 0 40 40
2 12 0 200 200
ps:Please consider posting code to reproduce your data so that you can get quicker response.