Rounding Up Number to the Nearest Value on Another Table - ms-access

is this something that's possible to achieve in Ms. Access?
I have two table, first table is a calculated product requirement:
Second Table is the fixed adjusted rate specific to each product (since there's a fix output rate from the equipment we used to apply the product and have to split them in a very specific rate)
The goal would be to generate this table (so I can link it to the table 2 and get a specific split rate required by the equipment)
I have been cranking my head for hours but couldn't find a solution yet. Any help would be appreciated :)

Consider:
SELECT Table1.*, Table2.* FROM Table1
INNER JOIN Table2 ON Table1.Product = Table2.Product
WHERE Table2.Adjusted = (SELECT TOP 1 Adjusted FROM Table2
INNER JOIN Table1 ON Table1.Product = Table2.Product
WHERE Table1.Calculated <= Table2.Adjusted
ORDER BY Adjusted);

You can also try below query-
Select t.product, MIN(t.adjusted) as Adjusted from
(SELECT t1.Product, t2.Adjusted
FROM Table1 as t1 INNER JOIN Table2 as t2 ON t1.Product = t2.Product
WHERE (t2.Adjusted>t1.calculated)) as t Group by t.Product;

Related

How to join where duplicates are needed?

I have a table structure like this
Table1
PersonID, referrer
Table2
Event_A_ID, PersonID, status
Table3
Event_B_ID, PersonID, status
I want to get the event status for everyone from table one with referrer=X by joining all of the event tables like Event_A...K and checking for PersonID. Since people can appear in multiple events we can have cases like this
PersonID=1001, EventA_ID, referrer=X, status
PersonID=1001, EventB_ID, referrer=X, status
PersonID=1001, EventK_ID, referrer=X, status
PersonID=1002, ...
PersonID=1003, ...
But I am not sure how to JOIN all of the event tables since the IDs can be duplicates (and are desired).
I tried to make a separate select and use the in syntax
...
WHERE 1=1
AND PersonID IN (SELECT PersonID from table1 where referrer=X)
But then I realized everything before will be an aggregate of events.
Should I start with the SELECT from Table1? Select the valid IDs first and then select from the events after? If so, how do I JOIN? I feel like ideal looks like union of all the event tables and then select
You can use union all for row wise implementation of data or you can also use inner joins between tables if there is not much table events. This will represent data in column format.
SELECT * FROM tb2 AS t2 INNER JOIN tb3 t3 ON t2.personId = t3.personId INNER JOIN tb1 t1 ON t1.personId = t2.personId WHERE t1.refer='refer1'
There can be many other approach too depending on the number of tables you want to join.
You should also consider to use a better relations among your db tables as your current scenario will lead you to have as many tables as many events you have. This will create slowness in retrieving the data for multiple events.
use union all and then apply join
select a.person_id,a.referrer,b.eventID,b.PersonID,b.status from Table1 a inner join
(
select Event_A_ID as eventID, PersonID, status from Table2
union all
select Event_B_ID as eventID, PersonID, status from Table3
)b on a.personid=b.personid
You can do something like this with left joins:
SELECT t1.PersonID, t1.referrer,
t2.Event_A_ID, t2.status as status_a,
t3.Event_B_ID, t3.status as status_b
.
.
.
FROM table1 t1
LEFT JOIN table2 t2 ON t2.PersonID = t1.PersonID
LEFT JOIN table3 t3 ON t3.PersonID = t1.PersonID
.
.
.
WHERE t1.referrer = 'x'

How to Join two tables and sum their columns across in Mysql based on distinct column

I am joining two tables by the regNo column. I want to add the points of Table1.points and Table2.points where the regNo matches and just incase it doesn't match I also want it be included with its points in the list as shown in the image bellow
I have read through the existing problems but not finding the solution to this e.g How can I sum columns across multiple tables in MySQL?
(
SELECT `Ex`.regNo,(`In`.`points`+`Ex`.`points`) AS 'Points'
FROM Table1`In`
LEFT JOIN Table2`Ex` ON `In`.`regNo` = `In`.`regNo`
)
UNION
(
SELECT`Ex`.regNo,(`In`.`points`+`Ex`.`points`) AS 'Points'
FROM Table1`In`
RIGHT JOIN Table2`Ex` ON `In`.`regNo` = `In`.`regNo`
);
I want it to give the list arranged as per unique (DISTINCT) regNo
You need UNION followed by GRoUP BY:
SELECT regNo, SUM(points) AS total
FROM (
SELECT regNo, points
FROM Table1
UNION ALL
SELECT regNo, points
FROM Table2
) AS u
GROUP BY regNo
You are looking for a FULL JOIN between both tables.
SELECT
COALESCE(t1.id, t2.id) id
COALESCE(t1.regNo, t2.regNo) regNo
COALESCE(t1.points, 0) + COALESCE(t2.points 0) points
FROM
table1 t1
FULL JOIN table2 t2 on t1.regNo = t2.regNo
NB : you did not specify what you expect to be done to generate the new id so by default the above query will display the table1.id if available, else table2.id.
If you would better generate a new, auto-incremented field, then :
SET #i=0;
SELECT
#i:=#i+1 id
COALESCE(t1.regNo, t2.regNo) regNo
COALESCE(t1.points, 0) + COALESCE(t2.points 0) points
FROM
table1 t1
FULL JOIN table2 t2 on t1.regNo = t2.regNo
Please check this. You need to use full outer join and null replacement before aggregation
select
COALESCE(table1.regno, table2.regno) regno,
sum(COALESCE(table1.points,0)) + sum(COALESCE(table2.points,0)) points
from Table1
full outer join Table2
on table1.regno = table2.regno
group by
COALESCE(table1.regno, table2.regno)

How to get the maximum and not duplicated data in my table in mysql?

I table data is like this:
id car_id create_time remark
6c3befd0201a4691 4539196f55b54523986535539ed7beef 2017-07-1 16:42:49 firstcar
769d85b323bb4a1c 4539196f55b54523986535539ed7beef 2017-07-18 16:42:49 secondcar
984660c4189e499 575d90e340d14cf1bef4349b7bb5de9a 2017-07-3 16:42:49 firstjeep
I want to get the newest data. It means if there have two same car_id, I want to get only one according the newest time. How to write?
I try to write this, but I find it may wrong. If the other record may have the same create_time? How to fix that?
SELECT * FROM t_decorate_car
WHERE create_time IN
(SELECT tmptime FROM
(SELECT MAX(create_time),tmptime,car_id
FROM decorate
GROUP BY car_id
) tmp
)
One canonical way to handle this is to join your table to a subquery which finds the latest record for each car_id. This subquery serves as a filter to remove the older records you don't want to see.
SELECT t1.*
FROM t_decorate_car t1
INNER JOIN
(
SELECT car_id, MAX(create_time) AS max_create_time
FROM t_decorate_car
GROUP BY car_id
) t2
ON t1.car_id = t2.car_id AND
t1.create_time = t2.max_create_time
By the way, if you want to continue down your current road, you can also solve this using a correlated subquery:
SELECT t1.*
FROM t_decorate_car t1
WHERE t1.create_time = (SELECT MAX(t2.create_time) FROM t_decorate_car t2
WHERE t2.car_id = t1.car_id)
You were on the right track but you never connected the subquery to the main query using the right WHERE clause.

Calculate quantity in a faster way than in this query

I need to calculate total available qty from the database, and for that I need to do joins with a couple of tables. I can not paste my whole query, but the following is the basic structure:
select sum(qty) as qty, field
from
(
(
select SUM(table1.qty) as qty , field
from
table1
left join table2 on table1.field = table2.field
left join table3 on table3.field = table2.field
where condition
group by fieldname
)
UNION ALL
(
selecy SUM(table1.qty) as qty,field
from
table1
left join table2 on table1.field = table2.field
left join table3 on table3.field = table2.field
where condition
group by fieldname
)
UNION ALL
(
select SUM(table1.qty) as qty, field
from
table1
left join table2 on table1.field = table2.field
left join table3 on table3.field = table2.field
where condition
group by fieldname
)
...
..
12 times
) as temp
LEFT JOIN another_main_table ON another_main_table.field = temp.field
I have taken care of indexes of each table, but there are some unions which are taking longer time than expected. There are around 45 tables used in this query and all are examined fully. Some tables have around 2.6 million records.
Can you please suggest me how I can get the result in 1/2 seconds? As of now I am getting the result in around one minute.
Since your given example one can not properly justify the proper solutions, but still if I roughly examine your query, you have used LEFT JOIN, So this will take a longer time compare to INNER JOIN.
So, Use INNER JOIN if your data permits

SQL SELECT from multiple tables or JOIN

I have two tables from which I need to get data in the same SELECT output. The thing is that I need to limit the amount of results.
Say I have an ID column that is unique in table1, but in table2 it has many rows with that ID.
Now I just want to list how many different IDs I have in table1 and some other information stored in table2.
How can I get the desired output I show in the end?
To make my idea clear I used a "messenger" database for an example.
Tables
T1
Id_thread Date
1 13Dic
2 12Dic
T2
Id_thread Message Name
1 Hi Someone
1 Hi to you Someone
2 Help me? Someother
2 Yes! Someother
Desired output
T1.Id_thread T2.Name T1.Date
1 Someone 13Dic
2 Someother 12Dic
I'd join and use distinct:
SELECT DISTINCT t1.id_thread, t2.name, t1.date
FROM t1
JOIN t2 ON t1.id_thred = t2.id_thread
Use a JOIN and GROUP BY:
SELECT t1.Id_thread, t2.Name, t1.Date
FROM t1
JOIN t2 ON t1.Id_thread = t2.Id_thread
GROUP BY t1.Id_thread
Note that if Name is the same for all rows in t2 that have the same Id_thread, that column probably should be in t1. If you fix that, you don't need the JOIN.
Try this:
SELECT DISTINCT T1.Id_thread, T2.Name, T1.Date
FROM T1
LEFT OUTER JOIN T2 ON T1.Id_thread = T2.Id_thread
select T1.Id_thread,T2.Name,T1.Date from T1
inner join T2 on T1.Id_thread = T2.Id_thread
group by T1.Id_thread
order by T1.Id_thread
You haven't specified how you want the limit the results from Table 2. Considering you just want one row, you can use a CROSS APPLY:
Select T1.Id_thread,T2Table.Name,T1.Date From T1
Cross Apply (Select Top 1 T2.Name From T2 Where T2.Id_thread=T1.Id_thread) T2Table
You can specify other conditions in the inner Select statement if you wish.