I am trying to update a table using data gotten from 3 tables.
table3 contains rates for different days. The rates aren't measured daily so I simply want to use the most recent date with respect to the table2 record which is what prompts the trigger.
So far this is what I have but I can't seem to get it to work.
I'm not sure I'm explaining myself properly to be honest.
CREATE TRIGGER `trigger1`
AFTER UPDATE ON `table2` FOR EACH ROW
UPDATE `table4`
inner join (SELECT o.`Name`,
o.Date,
(o.`value` * (m.`rate`)) total
FROM `table1` o
LEFT JOIN `table2` r
ON o.`Name` = r.`Name`
AND o.Date = r.Date
LEFT JOIN (SELECT * from `table3`
INNER JOIN (SELECT `Name`, Date
WHERE Date < r.Date
ORDER BY Date DESC
LIMIT 1) as y
ON table3.Date = y.Date AND table3.Name = y.Name) m
ON o.`Name` = m.`Name`
GROUP BY o.`Name`, o.Date)x
set `Contribution` = x.total
where (`table4`.Date) = x.Date and `table4`.`Well Name` = x.`Well Name`;
fiddle link : https://www.db-fiddle.com/f/pVqmhmM21XJARKhvFMP52B/4
Related
I have the following query that is doing an INNER JOIN within a select query to insert records into the table. The issue is instead of using the WHERE clause and listing the 3 t2.device I would like to see, I need to do another INNER JOIN with a table called tbl_Full_List.
Both tbl_Full_List and tbl1_device have a column called device so instead of the WHERE clause I would like it to show only devices that match in both tbl_Full_List and tbl1_device
INSERT INTO TEMP_Table1 (place, device, description, quantity)
(SELECT t1.place, t2.device, t2.description, Sum(t2.quantity) AS quantity
FROM Device_Table AS t1
INNER JOIN tbl1_device AS t2
ON t1.rma = t2.rma
WHERE t2.device IN ('PRT1030', 'PRT-23','PRT-20139')
AND t1.date_made = Current_Date()
GROUP BY t1.place, t2.device, t2.description ORDER BY place ASC, device ASC )
Without knowing how the two relate... I'm assuming FL.ID is the PK of full list and we have a Foreign Key to tbl1_devcice called FL_ID
INSERT INTO TEMP_Table1 (place, device, description, quantity)
(SELECT t1.place, t2.device, t2.description, Sum(t2.quantity) AS quantity
FROM Device_Table AS t1
INNER JOIN tbl1_device AS t2
ON t1.rma = t2.rma
INNER JOIN tbl_Full_List FL
ON FL.Device = t2.Device
WHERE t1.date_made = Current_Date()
GROUP BY t1.place, t2.device, t2.description ORDER BY place ASC, device ASC )
I have a query that is to pull at max one entry per day per specified id. The query is returning multiple values per day despite max being specified. I need some assistance tweaking this so that I only get one entry per day. The image shows a snippet of the data that is returned.
SELECT a.*
FROM turtle_derby AS a
INNER JOIN (SELECT turtle_id, DATE(`date`) AS day_date, MAX(`date`) AS maxdate
FROM turtle_derby
GROUP BY turtle_id, day_date) AS groupedtt
ON a.turtle_id = groupedtt.turtle_id
AND a.turtle_id = '175846'
AND a.`date` = groupedtt.maxdate
AND a.`date` > '2018-07-26'
ORDER BY `a`.`date`
Your data has multiple rows are at the same time of the day. If that time is the last time of the day, you'll get all of them. You need to get the maximum of two columns, date and loc_id. See SQL : Using GROUP BY and MAX on multiple columns
SELECT a.*
FROM turtle_derby AS a
INNER JOIN (
SELECT t2.turtle_id, MAX(t2.loc_id) AS max_id
FROM (
SELECT turtle_id, MAX(date) AS maxdate
FROM turtle_derby AS
GROUP BY turtle_id, DATE(date)
) AS t1
INNER JOIN turtle_derby AS t2
ON t1.turtle_id = t2.turtle_id AND t1.maxdate = t2.date
GROUP BY t1.turtle_id, t1.maxdate
) AS groupedtt
ON a.turtle_id = groupedtt.turtle_id
AND a.loc_id = groupedtt.maxloc
WHERE a.turtle_id = '175846'
AND a.`date` > '2018-07-26'
ORDER BY `a`.`date`
I have good working sql query but I need to select also atribute from table advert. I tried with inner join but it wasn't successful. So this query is ok but I need to select one atribute from table advert.
SELECT D.* FROM details
WHERE (D.name LIKE ?) AND (D.id_advert IN(
SELECT A.id
FROM advert A
WHERE A.status=1 and duration >= CURDATE()
ORDER BY duration DESC ))
You can change the in (subquery ) in a proper inner join and the is simple use the columns form table A
SELECT D.* , A.*
FROM details
INNER JOIN advert A ON D.id_advert = A.id
AND A.status=1
AND duration >= CURDATE()
WHERE D.name LIKE ?
SELECT *
FROM details D
INNER JOIN advert A ON D.id_advert = A.id
INNER JOIN place P ON A.id_place = P.id
WHERE (D.NAME LIKE ?)
AND ( D.id_advert IN (
SELECT A.id
FROM advert A
WHERE A.STATUS = 1 AND duration >= CURDATE()
ORDER BY duration DESC
)
)
Here "?" is for search key. This query work perfect.
i have these tables :
notice
id INT
cdate DATETIME
...
theme
id
name
notice_theme
id_notice
id_theme
I want to get the latest notices for each theme.
SELECT id_theme, n.id
FROM notice_theme
LEFT JOIN (
SELECT id, cdate
FROM notice
ORDER BY cdate DESC
) AS n ON notice_theme.id_notice = n.id
GROUP BY id_theme
The result is not good. An idea ? Thanks.
There are so many ways to solve this but I'm used to do it this way. An extra subquery is needed to separately calculate the latest cDate for every ID.
SELECT a.*, c.*
FROM theme a
INNER JOIN notice_theme b
ON a.ID = b.id_theme
INNER JOIN notice c
ON b.id_notice = c.ID
INNER JOIN
(
SELECT a.id_theme, MAX(b.DATE_CREATE) max_date
FROM notice_theme a
INNER JOIN notice b
ON a.ID_Notice = b.ID
GROUP BY a.id_theme
) d ON b.id_theme = d.id_theme AND
c.DATE_CREATE = d.max_date
SQLFiddle Demo
I'm working with a mysql query that is supposed to select all messages addressed or sent by the user. I need to group all messages with same UID so that I show a single thread for each differente user (this means it should eliminate all messages except the last with same UID). My problem is that I started using GROUP BY to do it but sometimes the row that remains is actually the older message instead of the latest.
This is what I was trying:
SELECT `UID`, `Name`, `Text`, `A`.`Date`
FROM `Users`
INNER JOIN (
(
SELECT *, To_UID AS UID FROM `Messages` WHERE `From_UID` = '$userID' AND `To_UID` != '$userID'
)
UNION ALL
(
SELECT *, From_UID AS UID FROM `Messages` WHERE `To_UID` = '$userID' AND `From_UID` != '$userID'
)
) AS A
ON A.UID = Users.ID
GROUP BY UID // This doesn't work
How can I show only the row with the most resent date per UID?
use DISTINCT and only use ORDER BY date
GROUP BY actually sometimes displays a random row, which isn't always commonly discussed.
you can try some thing like this:
select UID, Name, Text, c.date
from User
inner join (
select if(b.From_UID = '$userID', b.To_UID, b.From_UID) as UID,
*
from Messages as b
inner join(
select if(c.From_UID = '$userID', c.To_UID, c.From_UID) as UID,
max(c.date) as date
from Messages as c
where c.From_UID = '$userID' or c.To_UID = '$userID'
group by UID
) as d on d.date = b.date and d.UID = b.UID
) as e on e.UID = Users.id
)
or create a temp table / stored procedure to make life easier
Temp table
create temp table t
select if(From_UID = '$userID', To_UID, From_UID) as UID, * from Messages
select UID, Name, Text, date
from User
inner join (
select *
from t as t1
inner join(
select
t2.UID,
max(t2.date) as date
from t as t2
group by t2.UID
) as t3 on t3.date = t1.date and t3.UID = t1.UID
) as e on e.UID = Users.id