mysql query of 2 tables with inner join - mysql

I want to join Table A and Table B and display the most recent entry for each truck.
Table A columns: TA ID,Truck_Num,Serial_Num
Table B columns: TB ID,Serial_Num,Lat,Long,DateTime
Desired output
Truck | DateTime | Serial_Num | Lat | Long
1 | datestamp | 123 | 1 | -1
2 | datestamp | 456 | 2 | -2
using join query
SELECT a.Truck b.Lat b.Long b.DateTime FROM TB as b INNER JOIN TA AS a a.Serial_Num=b.Serial_Num;
I'v tried this but it is only giving me the most recent entry's time.
SELECT a.Truck b.Lat b.Long b.DateTime FROM TB as b INNER JOIN TA AS a a.Serial_Num=b.Serial_Num WHERE b.DateTime = (SELECT MAX(b.DateTime) FROM TB tb WHERE a.Truck_Num=Truck_Num);

This will show the most recent item per Truck. If the rest of the information that you want to show is the same in all rows then you can put it on the group by clause, if not then you have to do a join with this result
SELECT Serial_Num, MAX(DateTime) FROM TB GROUP BY Serial_Num;
This is the join you have to do if the data is not equal and you cannot put it on the group by clause
SELECT Truck_Num, Lat, Long, maxDateTime FROM TB as b
INNER JOIN TA AS a ON a.Serial_Num=b.Serial_Num
INNER JOIN (SELECT Serial_Num, MAX(DateTime) as maxDateTime
FROM TB GROUP BY Serial_Num) as c
ON b.Serial_Num=c.Serial_Num AND maxDateTime = DateTime

It's should work, you can check this question:
Select only one row with max field with One-Many Relationship Table
SELECT
ta.Truck,
tb.Lat,
tb.Long,
MAX(tb.DateTime),
ta.Serial_Num as Serial_Num
FROM
TB as tb
INNER JOIN
TA as ta
ON (tb.Serial_Num = ta.Serial_Num)
GROUP BY
tb.Serial_Num

Related

Optimize and reduce size of Union Select Query

My question is about how to optimize and reduce size of a sql query. I want to join more than 20 multiple queries using UNION, it is giving me the correct result as per the below logic, but I am looking for two things here
something more efficient
I already have 20 UNIONS in my query, and every month I have to add 2-4 UNIONS more which make this query very long so is there any way this query can be rephrased with less code
Select
'343' As 'Manual ID',
'24/07/2020' As 'Date',
A.ID,
O.Order_Name,
C.Customer_Name,
Q.Quantity
From Shipper A
Left Join Order O A.ID = O.ID
Left Join Customer C A.ID = C.ID
Left Join Quantity Q Q.ID = C.ID
where A.ID IN (1)
UNION
Select
'323' As 'Manual ID',
'24/08/2020' As 'Date',
A.ID,
O.Order_Name,
C.Customer_Name,
Q.Quantity
From Shipper A
Left Join Order O A.ID = O.ID
Left Join Customer C A.ID = C.ID
Left Join Quantity Q Q.ID = C.ID
where A.ID IN(2,3,4)
and so on ...
Result
Manual ID | Date | Shipper | Order Name | Customer Name | Qty
343 | 24/07/2020 | 1 | order1 | A | 5
323 | 24/08/2020 | 2 | order2 | B | 2
323 | 24/08/2020 | 3 | order3 | C | 1
323 | 24/08/2020 | 4 | order4 | D | 12
You can try this:
Select
CASE
WHEN A.ID IN(1) THEN '343'
WHEN A.ID IN(2,3,4) THEN '323'
END As 'Manual ID',
CASE
WHEN A.ID IN(1) THEN '24/07/2020'
WHEN A.ID IN(2,3,4) THEN '24/08/2020'
END As 'Date',
A.ID,
O.Order_Name,
C.Customer_Name,
Q.Quantity
From Shipper A
Left Join Order O A.ID = O.ID
Left Join Customer C A.ID = C.ID
Left Join Quantity Q Q.ID = C.ID
Where A.ID IN(1,2,3,4)
First suggestion is to move the parameters in to another table, then join on it. You can even make that an inline view if you don't want to use a real table...
Second suggestion is to use UNION ALL to avoid the costs of deduplication incurred by UNION.
SELECT
params.*,
O.Order_Name,
C.Customer_Name,
Q.Quantity
FROM
(
SELECT '343' As 'Manual ID', '24/07/2020' As 'Date', 1 AS A_ID
UNION ALL SELECT '323' As 'Manual ID', '24/08/2020' As 'Date', 2 AS A_ID
UNION ALL SELECT '323' As 'Manual ID', '24/08/2020' As 'Date', 3 AS A_ID
UNION ALL SELECT '323' As 'Manual ID', '24/08/2020' As 'Date', 4 AS A_ID
)
AS params
INNER JOIN Shipper A ON A.ID = params.A_ID
Left Join Order O ON A.ID = O.ID
Left Join Customer C ON C A.ID = C.ID
Left Join Quantity Q ON Q.ID = C.ID
Alternatively, don't recompute this every month. Write a new query each month, and insert the results into another table?
If you just want to go for query the better way would be to use the case when statement but every now and then you need to keep updating the query adding new cases.
Another, optimized solution will be to create a new table to store
Manual ID, Date, (Common) ID present in Shipper (Table). Then create a view to join all above tables with new Table.
New Table
Manual ID | Date | ID |
343 | 24/07/2020 | 1 |
323 | 24/08/2020 | 2 |
323 | 24/08/2020 | 3 |
323 | 24/08/2020 | 4 |
Then Create a View Joining all Tables including new new table with ID.
In this you just need add new value to new table and you will complete result in view it self.
CREATE VIEW MY_VIEW
AS
SELECT * FROM
(
Select
T.[Manual ID],
T.[Date],
A.ID,
O.Order_Name,
C.Customer_Name,
Q.Quantity
From Shipper A
Left Join Order O A.ID = O.ID
Left Join Customer C A.ID = C.ID
Left Join Quantity Q Q.ID = C.ID
Left Join NewTable T T.ID = A.ID
)
Now just insert value in new table and fetch complete data from MY_VIEW. It will give the same result as you are excepting.

MYSQL Query to get the SUM and LAST record value in Group BY

I have two table 1 is account and the other one is account_spend_history AS ash account is the parent/master table and the shin a sub table. and has a relationship OneToMany with account(account table id is a foreign key in ash as account_id). please see the image
Now I need to get the account.id, total spend (which is the sum of the amount_spend with same account_id) and last spend (is the last record inserted in ash table against and account_id i.e. amount_spend value corresponding with MAX(ash.id)), that is
id | spend_total | last_spend
---------------------------------
1 | 30 | 18
2 | 280 | 120
3 | 20 | 20
SELECT a.id, SUM(ash.amount_spend) AS spend_total
FROM accounts as a
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
GROUP BY a.id
Im getting the account.id and sum of ash.amount spend, but I also need last spend. How to get that?
Thanks.
Here's one option using a correlated subquery:
SELECT a.id, SUM(ash.amount_spend) AS spend_total,
(SELECT amount_spend
FROM account_spend_history as ash2
WHERE ash2.account_id = a.id
ORDER BY ash2.id DESC LIMIT 1) as last_spend
FROM accounts as a
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
GROUP BY a.id
You can get the MAX(ash.id) as well in your query and then use it to join back to account_spend_history table:
SELECT id, spend_total, amount_send as last_spend
FROM (
SELECT a.id, SUM(ash.amount_spend) AS spend_total, MAX(ash.id) AS ash_id
FROM accounts as a
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
GROUP BY a.id) AS t1
INNER JOIN account_spend_history AS ash ON ash.id = t1.ash_id
You can use a in clause and subquery
SELECT a.id, SUM(ash.amount_spend) AS spend_total, x.last_spend
FROM accounts as a
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
INNER JOIN (
select account_id, last_spend, start_date
from account_spend_history
where (start_date, acoount_id) in (
select max(start_date), account_id
from account_spend_history
group by account_id
)
) x on x.id = a.id
GROUP BY a.id

mysql count related table

Hi everyone I would like to count some entries, but I don't know if is possible to do it with joins. The situation is
I've got a tables
student_profile
Id | Name
---------
1 | Name1
2 | Name2
3 | Name3
student_application where profile_id is related to student_profile.id
Id | profile_id | Data
----------------------
1 2 data1
2 2 data2
3 2 data3
And table student_holiday
Id | app_id | date
-----------------------
1 2 2014-01-01
2 3 2014-02-02
So I'm getting all my student_application's with
Select sa.id, s.name From student_application sa
INNER JOIN student_profile s ON s.id = sa.profile_id
I would like to count how many holidays has a student, but I don't have profile_id in student_holiday table. There I've got app_id, so I can't do Left join student_holiday sh ON sh.app_id = sa.id, this wouldn't give the right number.
here sqlfiddle
Thanks in advance for any help.
SELECT student_profile.ID, student_profile.Name, student_holiday.ID
FROM (student_profile INNER JOIN student_holiday ON student_profile.ID = student_holiday.ID) INNER JOIN student_application ON student_holiday.ID = student_application.ID;
Here you go!!! Match ID in all the table as instructed above which is the key to get holidays.
OR match with profile_id
**SELECT student_profile.ID, student_profile.Name, student_holiday.ID
FROM (student_profile INNER JOIN student_holiday ON student_profile.ID = student_holiday.ID) INNER JOIN student_application ON Trim(student_holiday.ID) =
student_application.profile_id;**
Here is your data with the 3rd table joined:
Select s.name, sa.some_data as application_data,h.some_data as holiday_data
From student_application sa
INNER JOIN student_profile s ON s.id = sa.profile_id
INNER JOIN student_holiday h ON h.app_id = sa.id;
Note this will only return records that have an application with a holiday associated.
Then to count them, we just:
Select s.name, sa.some_data as application_data,count(h.some_data) as holiday_data
From student_application sa
INNER JOIN student_profile s ON s.id = sa.profile_id
INNER JOIN student_holiday h ON h.app_id = sa.id
GROUP BY s.name
Note this answer is based on the fieldnames you used in your sqlfiddle (Data = some_data and date = some_data)

Very, very slow query

I have this query:
SELECT
a.id,
a.name,
count(b.id),
count(c.id),
count(e.id),
count(f.id)
FROM
organizations a
LEFT JOIN vessels b ON a.id = b.organization_id
LEFT JOIN licences c ON a.id = c.organization_id
LEFT JOIN fleets e ON a.id = e.organization_id
LEFT JOIN users f ON a.id = f.organization_id
GROUP BY a.id;
In all tables there's a proper index (on the primary index, and organization_id), there's about 80 rows in organizations, 400 in fleets, 2900 in vessels, 3000 in licences and 10 in users
This query doesn't even succeed, it's stuck on copying to temp table
How should I re-work this query to make it work (fast) ?
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a index PRIMARY 4 1
1 SIMPLE b ref organisation_id organisation_id 4 fuel.a.id 70 Using index
1 SIMPLE c ref organisation_id organisation_id 4 fuel.a.id 15 Using index
1 SIMPLE e ref organisation_id organisation_id 4 fuel.a.id 5
1 SIMPLE f ref organization_id organization_id 5 fuel.a.id 1 Using index
Your joins do not depend on each other, that's why the temp tables are exploding.
A simple fix is to make:
SELECT
a.id,
a.name,
(select count(*) from vessels b where a.id = b.organization_id group by b.organization_id),
(select count(*) from licenses b where a.id = b.organization_id group by b.organization_id),
(select count(*) from fleets b where a.id = b.organization_id group by b.organization_id),
(select count(*) from users b where a.id = b.organization_id group by b.organization_id),
FROM
organizations a
It will be far more faster, if you do it like this:
SELECT
a.id,
a.name,
v.total,
w.total,
x.total,
y.total
FROM
organizations a
LEFT JOIN (select b.organizantion_id, count(*) total from vessels b group by b.organization_id) v on v.organization_id=a.id
LEFT JOIN (select b.organizantion_id, count(*) total from licenses b group by b.organization_id) w on w.organization_id=a.id
LEFT JOIN (select b.organizantion_id, count(*) total from fleets b group by b.organization_id) x on x.organization_id=a.id
LEFT JOIN (select b.organizantion_id, count(*) total from users b group by b.organization_id) y on y.organization_id=a.id

how to write the sql statement?

I have 3 tables as follows in my database. this is used to a application just like foursqure. i need help with the problem of writing the sql statement i have asked in the bottom of this.
thank you
user_details
user_id | fname
----------------
1 | Losh
8 | Dush
9 | Rosh
10 | NELL
friends
user_idf |user_idff
----------------
1 | 8
8 | 9
10 | 1
Check_in
check_in_id |user_id | place | date
--------------------------------------------
1 | 8 | Hotel | 01/01/2012
2 | 9 | Home | 05/01/2012
3 | 1 | Junction | 08/01/2012
4 | 1 | Rest | 11/01/2012
5 | 9 | Hotel | 15/01/2012
6 | 8 | Home | 15/01/2012
i get the user's who are friends with 8 and user 8 details AND the check in places as follows
SELECT a.`user_id`, a.`fname` , b.*
FROM `user_details` a, `check_in` b
WHERE (b.user_id = 8
OR b.user_id in (select user_idf from friends where user_idff = '8' union select user_idff from friends where user_idf = '8')) AND b.user_id = a.user_id
how do i write the sql to select who are friends with 8 and user 8 details AND the last check in place of those users
explanation::
i seeks for a answer such as
user id name place date
1 LOSH Rest 11/01/2012
8 DUSH HOME 15/01/2012
9 ROSH HOTEL 15/01/2012
Join it to the table returned by:
(SELECT `user_id`, `place` FROM Check_in GROUP BY user_id ORDER BY `date` DESC)
That should give you one entry per user, and since it's sorted in reverse by date, that entry should be the most recent.
But when i group by it gives me the first dates not the latest date
How about this:
(SELECT user_id, place
FROM (SELECT * FROM Check_in ORDER BY `date` DESC) tmp
GROUP BY user_id)
SELECT user_id, fname, c.place
FROM user_details u
INNER JOIN (SELECT IF(user_idff = 8, user_idf, user_idff) AS user_id
FROM friends
WHERE (user_idff = 8 OR user_idf = 8)
) f
ON u.user_id = f.user_id
LEFT JOIN (SELECT c1.user_id, place
FROM Check_in c1
LEFT JOIN Check_in c2
ON c1.user_id = c2.user_id AND
c1.date < c2.date
WHERE c2.date IS NULL
) c
ON u.user_id = c.user_id;
This doesn't break ties but it's a straighforward way of answering your question.
EDIT
I just re-read you question and I see that you want user 8 info also. It's not clear whether you want user 8 as a separate row or with info in line with the friends' rows.
select *
from
friends as f inner join check_in as ci on ci.user_id = f.user_idff
inner join user_details as ud on ud.user_id = f.user_idff
inner join user_details as ud8 on ud8.user_id = f.user_idf
where
f.user_idf = 8
and date = (
select max(date)
from friends as f2 inner join check_in as ci on ci.user_id = f2.user_idff
where f2.user_idf = f.user_idf
)
EDIT 2
You request may be a small bit unclear about determining which check-in location to return. Use this option if you want the latest location of each friend individually. The first query finds the most recent location among all friends. Obviously these are two variations on an identical theme.
select *
from
friends as f inner join check_in as ci on ci.user_id = f.user_idff
inner join user_details as ud on ud.user_id = f.user_idff
inner join user_details as ud8 on ud8.user_id = f.user_idf
where
f.user_idf = 8
and date = (
select max(date)
from check_in as ci
where ci.user_id = f.user_idff
)
(SELECT a.user_id, a.place, b.fname, a.date, a.time, a.check_in_id
FROM (SELECT * FROM check_in ORDER BY date DESC) as a, user_details as b
WHERE a.user_id = b.user_id AND (a.user_id in (select user_idf from friends where user_idff = '8' union select user_idff from friends where user_idf = '8') OR a.user_id = 8)
GROUP BY a.user_id)
above query gave me the required answer.
thank you all for the help given