MS access: Merging 02 tables into 01 - ms-access

I am using Microsoft Access database (Please don’t suggest me to use some other database since I donot have that choice in this particular situation).
I have 02 tables as below.
Table 1: Purchase Order
PO_No, Item_No, Order_Qty
01 a 10
01 b 5
02 a 8
02 b 2
Table 2: Stock
Item_No, Batch_No, Qty
a 0001 5
a 0002 50
b 0003 10
=> I would like to create something like this:
Item_No, Batch_No, Ship_Qty, PO_No
a 0001 5 01
a 0002 5 01
a 0002 8 02
b 0003 5 01
b 0003 2 02
Please help!

EDIT: Make-Table Example
You still need to read through at least some of the links that I posted earlier but I don't think you we getting what I meant so here's an animation to show you the steps.
Create Select Query from the 2 tables
Test Select Query to make sure output is what you want in new table
Convert Select Query to Make Table Query
Execute Make Table Query
This is a very short process, just a few minutes.
Good Luck.
STEP 1:
First you need to create a Select Query using both of your tables, joined together, similar to this: (See the tutorials at the bottom of this answer.)
STEP 2:
Once the Select Query is producing the data how you want it organized, you can convert it to a Make Table query:
Tutorials:
Skillshare : Intro to Access - Microsoft Access Basics for Beginners
Microsoft.com : Introduction to Queries: Access
YouTube : Acccess 2016: Multi-table queries
Office.com : Join tables and queries
Microsoft.com : Create a make-table query

Well kudos to ashleedawg for that effort.
In the end - your very first step must be to make a select query that will result in the records/fields correctly of what will be the new table.
The second step is then to make a Make Table Query. But that can wait for now.
In glancing at your 2 source tables, and the resulting 3rd table you seek - - I did not see any join logic that would create that result. If you can explain what is the logic - - then help with that select query can be provided.

Related

MySQL how to update aggregated table (based on another table) automatically? [duplicate]

This question already has answers here:
How do I create a view in MySQL? [closed]
(2 answers)
Closed last month.
I have a table (table1) in a MySQL database with data similar to this:
Category
total_sold
revenue
profit
fruit
32
200
150
veggies
12
50
23
chips
23
170
110
fruit
43
300
180
chips
5
25
15
New data is inserted into the table regularly through a python script using sqlalchemy (appends csv files to the mysql table using Pandas).
I want to create an aggregated table (table2) based on category using a query, and I want the aggregation to update whenever I add new records to the table1.
For example after executing the query:
CREATE TABLE table2 AS
SELECT Category, AVG(total_sold) avg_sold, AVG(revenue) avg_revenue, AVG(profit) avg_profit
FROM table1
GROUP BY 1
I get table2 as:
Category
avg_sold
avg_revenue
avg_profit
fruit
37.5
250
165
veggies
12
50
23
chips
14
97.5
62.5
Now if I insert several new records to table1, how can I make table2 update automatically? (recalculate all aggregations based on all table1 records including the newly added ones)
I read about creating a trigger, but I'm not sure how to execute it or if it would work in a case like this.
I Would appreciate any help.
MySQL does not support "materialized views". You have to do the aggregation yourself. Build a separate Summary Table and keep it updated. A simple IODKU is one way to do that maintenance; the link discusses other, such as a nightly batch update of the day's data.

MYSQL Group in one table and multiply with numbers from another table

I have two tables as below:
goods_in:
in_id|pid|in_num|in_date
1 1001 10 2020-06-28
2 1002 20 2020-06-28
3 1001 20 2020-05-25
......
stock:
stock_id|pid|num|price
1 1001 10 5
2 1002 15 6
3 1003 20 7
...
The "goods_in" table stores the records that all goods come into warehouse, the pid in this table is the same as in table "stock" which is the product ref code. There will be multiple records for each product in "goods_in" table.
The "stock" table stores all the SKU we are holding and the current stock level as well as the product cost.
What I'm trying to do is:
Group by pid AND date (Year+Month) from "goods_in" so I get sub-total number of goods booked-in in each month.
Multiple sub-total with stock.price.
Get total amount of above, something like SUMPRODUCT in excel.
Exports to html table or excel.
I've tried several answers from SO with GROUP BY/ROLLUP/JOIN, and apparently I haven't made it right as expected. I can make this simple if I just add a cost column to the "goods_in" table but that would make it untidy.
I'm still on my learning curves with MYSQL, forgive me if this looks simple to you guys.
Thanks.
Ken
Hard to know for sure what you want, but it sounds something like this:
select
pid,
year(in_date) as year,
month(in_date) as month,
sum(goods_in.in_num * stock.price)
from goods_in
join stock using (pid)
group by pid, year, month
For exporting to html or excel, you are best off asking a separate question.

Select and compare two columns from different tables to find matched records

I'm just learning PHP and MySQL and I have two tables in the same database : FirstYear , SecondYear that have a structure like this :
StudentId |Math | Physics StudentId1 | Math1 | physics1
Joe 10 14 Alan 12 17
Alan 13 17 Smith 11 13
Smith 9 9 Joe 10 15
Is it possible to write a query that select and compare the two columns StudentId , StudentId1 to find matched records and if for example Joe=Joe after that compare records of math with math1 and physics with physics1 that are in the same row as matched records of StudentId with StudentId1 ;the idea of this query is to study the improvement of same student from first year to the second one ,Thanks .
Yes, it is possible but you have to complete SQL fundamental course.
In this situation you have to know about JOIN. Such as, Inner Join, Left Join, Right Join, Full Join etc. Also, compare with unique id, not name. Because, name always duplicate. It is not good practice. So, Know about primary key and foreign key.
However,
Query-
SELECT * FROM FirstYear INNER JOIN SecondYear ON FirstYear.StudentId = SecondYear.StudentId1 WHERE FirstYear.id = 1
Something like that, alternatively, you can try to another logic.

T-SQL query procedure-insert

I am wondering if any of you would be able to help me. I am trying to loop through table 1 (which has duplicate values of the plant codes) and based on the unique plant codes, create a new record for the two other tables. For each unique Plant code I want to create a new row in the other two tables and regarding the non unique PtypeID I link any one of the PTypeID's for all inserts it doesnt matter which I choose and for the rest of the fields like name etc. I would like to set those myself, I am just stuck on the logic of how to insert based on looping through a certain table and adding to another. So here is the data:
Table 1
PlantCode PlantID PTypeID
MEX 1 10
USA 2 11
USA 2 12
AUS 3 13
CHL 4 14
Table 2
PTypeID PtypeName PRID
123 Supplier 1
23 General 2
45 Customer 3
90 Broker 4
90 Broker 5
Table 3
PCreatedDate PRID PRName
2005-03-21 14:44:27.157 1 Classification
2005-03-29 00:00:00.000 2 Follow Up
2005-04-13 09:27:17.720 3 Step 1
2005-04-13 10:31:37.680 4 Step 2
2005-04-13 10:32:17.663 5 General Process
Any help at all would be greatly appreciated
I'm unclear on what relationship there is between Table 1 and either of the other two, so this is going to be a bit general.
First, there are two options and both require a select statement to get the unique values of PlantCode out of table1, along with one of the PTypeId's associated with it, so let's do that:
select PlantCode, min(PTypeId)
from table1
group by PlantCode;
This gets the lowest valued PTypeId associated with the PlantCode. You could use max(PTypeId) instead which gets the highest value if you wanted: for 'USA' min will give you 11 and max will give you 12.
Having selected that data you can either write some code (C#, C++, java, whatever) to read through the results row by row and insert new data into table2 and table3. I'm not going to show that, but I'll show how the do it using pure SQL.
insert into table2 (PTypeId, PTypeName, PRID)
select PTypeId, 'YourChoiceOfName', 24 -- set PRID to 24 for all
from
(
select PlantCode, min(PTypeId) as PTypeId
from table1
group by PlantCode
) x;
and follow that with a similar insert.... select... for table3.
Hope that helps.

How can I shift and duplicate rows in MS Access?

I am trying to make a calculation in MS Access that uses the previous periods values. My approach is to make a second field for each variable that I am working with and to shift the table.
If my query qry1 is:
aa bb cc
--- --- ---
12 34 56
78 91 01
My result should be:
aa0 bb0 cc0 aa1 bb1 cc1
---- ---- ---- ---- ---- ----
12 34 56 NULL NULL NULL
78 91 01 12 34 56
NULL NULL NULL 78 91 01
I am encountering two problems:
I tried adding the empty rows on top and at the bottom in two extra queries and somehow rows get lost.
When I combine these two queries using JOIN I get a cartesian product, i.e. the rows are not side by side.
You are trying to use Ms Access like you would use Excel. It might be easier to do what you want using an Excel macro + formulae.
The power of access is that it is a SQL system (which is why you get a Cartesian product when you JOIN, that's the idea) it sounds like what you want could be done pretty easily with a flat spreadsheet. You could write VBA to add rows for you if want to automate that part.
If that doesn't help, then maybe describe in more detail what you actually want to accomplish.
EDIT:
I missed the part in your post where you said you were doing a calculation using previous values. This is how I would accomplish that within a single Access query:
qry_Data: (based on tbl_Data)
id aa bb cc calc
--- --- --- --- ---
01 12 34 56 =Dlookup("aa","tbl_Data","id = " & [id] - 1)
02 78 91 01 =Dlookup("aa","tbl_Data","id = " & [id] - 1)
You shouldn't add columns to your table as part of a normal operation. Access tables should be set up to hold all the data they will ever need to (until your model changes). If you are designing an operation in access and it seems like it should add columns, then you should be adding a linked table or using a calculated field. In the above example, the field calc will be equal to the value "aa" from the record with an ID# one less than the current record. This will only work if there is a record with that ID#, so if there is a possibility of there being a gap in ID numbers you shouldn't use this exact method. Since you said that the table is sorted on specific critiera you might need to use a different method to determine the previous record. One way to do that in VBA would be like this:
Dim RS as Recordset
Set RS = CurrentDB.QueryDefs("myQuery")
RS.MoveLast
RS.MovePrevious
At that point you are on the second to last record and can access any values from that record with:
RS.Fields("aa")
Without knowing exactly what you're trying to do I can't make any more specific recommendations except that if you familiarize yourself with basic SQL concepts and you will find working in Access much easier.