I have been attempting to update a column based on a group by a select from one table into another table. The below subquery on the set statement works but only for one date because if I use a date range I get an error of "subquery returns more than 1 row".
I instead want to run that on a date range fetching the group by for each day (from "Monthly" table) inserting each matching row by day into "Dayfile" table. The dayfile table has a row for each date with the "LogDate column" as date and the monthly table is a log file of minute-by minute values where the "LogDateTime" data type is datetime.
UPDATE
Dayfile
SET
MaxFeelsLike =
(SELECT MAX(FeelsLike)
FROM Monthly, Dayfile
WHERE DATE(LogDateTime) = "2018-10-04"
AND DATE(LogDateTime) = DATE(LogDate)
GROUP BY DATE(LogDateTime)
);
You should use a JOIN rather using the subquery as a value.
UPDATE Dayfile AS d
JOIN (
SELECT DATE(LogDateTime) AS date, MAX(FeelsLike) AS feels
FROM Monthly
GROUP BY date
) AS m ON DATE(d.LogDate) = m.date
SET d.MaxFeelsLike = m.feels
Include LIMIT 1 at the end of your subquery
Related
I have a table named "MI_APR". I am bringing in data from a "calendar" table using a LEFT JOIN. This data being transferred is a column of DATES with column name "First_Date".
ALTER TABLE MI_APR
ADD COLUMN `Profile` CHAR(8) NOT NULL DEFAULT 'MI',
ADD COLUMN First_Date DATE,
CHANGE `Location Label` `Label`TINYTEXT;
UPDATE MI_APR LEFT JOIN calendar on MI_APR.`Period` = calendar.`Unique_Period`
SET
MI_APR.`First_Date` = calendar.`First_Date`
After the join has happened, I need some help sorting the "FIRST_DATE" column in ASC , which is a simple date column in format YYYY-MM-DD.
I then need to add a new column named INDEX, that has values starting from 0 for the earliest month, counting upwards, based on the Month number in that DATES column in the MI_APR table. The index will run continue to run upwards even across years. See example below :
EXPECTED OUTPUT
FIRST_DATE INDEX
2018/10/01 0
2018/11/01 1
2018/12/01 2
2019/01/01 3
Thank you
You can either calculate your index on the fly with this query:
SELECT First_Date, TIMESTAMPDIFF(MONTH, earliest_date, First_Date) AS Index
FROM MI_APR
, (SELECT First_Date AS earliest_date FROM MI_APR ORDER BY First_Date ASC LIMIT 1) fancy_alias;
If the result pleases you, you can transform it into an update. My advice is though, that you shouldn't store data, which can easily be calculated on the fly!
UPDATE MI_APR
, (SELECT First_Date AS earliest_date FROM MI_APR ORDER BY First_Date ASC LIMIT 1) fancy_alias
SET `Index` = TIMESTAMPDIFF(MONTH, earliest_date, First_Date);
Regarding your comment "Also I would like the table to be sorted and then updated".
There is no order in a table you can rely on. If you want a result from a select sorted, use the ORDER BY clause.
I'm trying to implement an IF (SELECT) statement in an SQL query. The query looks like this so far:
SELECT *, IFNULL(duree_corrigee,duree) as
duree_final, IF((SELECT COUNT(*) FROM pointage_interval WHERE panier=1
AND pointage_employe_id = 'FH'
AND semaine = 23 GROUP BY date)>=1,
1,0) as panier_final FROM pointage_interval P
JOIN
employe E ON P.pointage_employe_id
= E.employe_id JOIN chantier C
ON C.chantier_id=P.pointage_chantier_id
WHERE (pointage_employe_id = 'FH'
AND semaine = 23)
When I run it, it returns me: #1242 - Subquery returns more than 1 row
Basically I can have many rows with the same date. One column is "panier", and can be either 1 or 0. I would like to create a new column panier_final, which takes the value 1 or 0 based on if there is a least one column with value of panier = 1 for the same day. So all the rows with this same date will get the same value 1 or 0 for panier_final.
If I run the query without the IF (SELECT), as this:
SELECT *, IFNULL(duree_corrigee,duree) as
duree_final FROM pointage_interval P
JOIN
employe E ON P.pointage_employe_id
= E.employe_id JOIN chantier C
ON C.chantier_id=P.pointage_chantier_id
WHERE (pointage_employe_id = 'FH'
AND semaine = 23)
I get this output:
As you can see, there are two rows for the same day, 2017-06-07. So I would like my IF (SELECT) query to create a new column (panier_final) for each row, the value of this new column being the same for each same date's rows. In this case each row would get a new column panier_final, the value being 1 since that for each given date, the values panier are equal or superior to 1
add this condition to your subquery : date = P.date
your subquery will be like this :
SELECT COUNT(*) FROM pointage_interval WHERE panier=1
AND pointage_employe_id = 'FH' AND date = P.date
AND semaine = 23 GROUP BY date
and i think you could remove GROUP BY date
I am trying to update a table with WTD values for specific date. Everytime while updating it I have to enter the date manually in the condition. Is there any method to pass the date values dynamically in conditions such that my WTD values get updated in one go?
Here is my update query:
update table1, (Select sum(sales) as sales from table1 where
day between subdate('2016-09-01', Interval weekday('2016-09-01') Day) and '2016-09-01') src
set WTD_Sales=src.sales
where day='2016-09-01'
So here I want to replace the statically added date '2016-09-01' with a variable that can fetch date from same table itself row by row dynamically
Your query looks wrong to me. Change it like below using a JOIN. Assuming there is a id column present in the table.
update table1 t1
join ( Select sum(sales) as sales, id, some_date_column
from table1
where day between subdate('2016-09-01', Interval weekday('2016-09-01') Day)
and '2016-09-01') src on t1.id = src.id
set t1.WTD_Sales = src.sales
where t1.day = src.some_date_column;
I have a table "T" that contains Date as one of the column.
In the table, there are multiple rows associated with single date entry.
I want an query that will give me all the rows associated with the latest date available in the table.
select * from table_name where date = (select max(date) from table_name);
If the column type is datetime then use this query
SELECT * FROM T
WHERE CAST(<DateColumn> AS DATE) = (SELECT MAX(cast(<DateColumn> AS DATE)) FROM T)
and if column type is just date then use this query
SELECT * FROM T
WHERE <DateColumn> = (SELECT MAX(<DateColumn>) FROM T)
Group function Max when used on date it results in latest date.
SELECT <<Columns from table>> T where date = (select max(date) from T);
I want to pull specific rows from a table where the date matches a certain date. First I'm converting the date string to date format, here's the query:
SELECT id, str_to_date(candidate.AddDate,"%d/%m/%Y") n FROM candidate WHERE n='2016-01-01';
But I get the error "Unknown column 'n' in WHERE clause"
How do I make the query use the result of str_to_date in the where clause?
You cant use the alias on the same level, because isnt created at that time
SELECT id,
Str_to_date(candidate.adddate, "%d/%m/%y") n
FROM candidate
WHERE Str_to_date(candidate.adddate, "%d/%m/%y") = '2016-01-01';
Or create a subquery
SELECT *
FROM (
SELECT id,
Str_to_date(candidate.adddate, "%d/%m/%y") n
FROM candidate
) T
WHERE n = '2016-01-01';
I dont know if this is what you are trying to achieve.
SELECT id, adddate from candidate C where C.adddate = "2016-01-01"
Why cant you pull all the table rows where the given date is 2016-01-01. Is this what you want? Or something else. If you have stored the date as date field you dont really need to do str_to_time.
If it is stored as string then
SELECT * FROM ( SELECT id, DATE_FORMAT(STR_TO_DATE(candidate.adddate, '%d/%m/%Y') x FROM candidate
) C WHERE x = '2016-01-01';