Im working on calculating through MySQL. The next values are day values and now I want to see what the difference is between the last row and 1 day (= 96 quarters) earlier. The line below is for the last and newest value:
SET #now = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 1);
The next line is the value of 1 day ago. This is the same as 96 quarters.
SET #yesterday = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 95,1);
Now I want to minus the newest value with the value 1 day ago so I know the difference between those values. The code below is what ive created right now:
CREATE EVENT Value_Test
ON SCHEDULE EVERY 15 MINUTE
STARTS '2017-08-20 16:36:00'
DO
SET #now = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 1);
SET #yesterday = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 95,1);
SET #value = (#now - #yesterday);
INSERT INTO solar.measurements (nad, tag, value, timestamp) VALUES (18223, 'c18223.ValueDayTest', #waarde, CURRENT_TIMESTAMP());
When I want to execute this I get the following error: Error Code: 1048. Column 'value' cannot be null. Can someone help me by finding a solution? Thanks!
EDIT:
Answer to a question
Day Ago
Today
A SHOW CREATE EVENT Value_Test would show you that your event wasn't done properly.
Try this:
Delimiter //
CREATE EVENT Value_Test
ON SCHEDULE EVERY 15 MINUTE
STARTS '2018-08-20 16:36:00'
DO
BEGIN
SET #now = (SELECT value FROM solar.measurements WHERE `tag` LIKE
'%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC
LIMIT 1);
SET #yesterday = (SELECT value FROM solar.measurements WHERE `tag` LIKE
'%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC
LIMIT 95,1);
SET #value = (#now - #yesterday);
INSERT INTO solar.measurements (nad, tag, value, timestamp) VALUES (18223,
'c18223.ValueDayTest', #waarde, CURRENT_TIMESTAMP());
END
//
Delimiter ;
In case of the need to use compound statements inside an EVENT you need to put them under Begin/End
Related
The MariaDB / MySQL event scheduler does not use new data. It only works on data that was in the table at the time of creating the event. How do I make it use whatever is in the table at the time?
CREATE EVENT IF NOT EXISTS my_calculation
ON SCHEDULE EVERY 5 SECOND STARTS '2021-01-01 00:00:00'
DO
SET #c = (SELECT COUNT(*) FROM my_table);
SET #rownum = 0;
UPDATE my_table
SET rank = (100 / #c) * (#rownum:= 1 + #rownum)
ORDER BY another_column DESC LIMIT 100000;
its because in your event query you have this line :
ORDER BY progress DESC LIMIT 100000;
So it always process only 100000 first rows that are ordered by progress column , remove LIMIT 100000 and you will be fine.
Respected all,
I have the following query that is executed only for a date by the filter that is noted, what I need is to run for all the dates in the table, and I can not find the way to indicate that function, I appreciate its special orientation:
update scraper_data_twitter as T1,
(
select Ntweets as Ntweets_var,
(
select COUNT(Ntweets) + 1
from scraper_data_twitter
where (NTweets > Ntweets_var)
and date = '2017-02-13'
) as rank
from scraper_data_twitter
where date = '2017-02-13'
group by SITE,
date
order by NTweets_var desc
) as A
set T1.rnk_Ntweets = A.rank
where T1.ntweets = A.Ntweets_var
Sorry, I have difficulty explaining my question and search for a previous answer. This is my problem -- I have a MySQL table with events
CREATE TABLE events {
id INT,
event INT,
date DATETIME
}
Data is being added a few times a week or month. I would like to see the statistical spread of time between two adjacent events. Something like:
Time difference between two events
1 day appart - 4 occurances
2 days apart - 2 occurances
n days apart - x occurances
It should be something like this, I guess, but calculating the time difference between events.
SELECT COUNT('id') AS 'no', ??? AS 'delta' GROUP BY FLOOR( 'delta' )
This piece of SQL code did it:
SET #old = NOW();
SELECT COUNT(`id`) AS `no`, query1.`delta` FROM
( SELECT `id`, `date`, DATEDIFF( #old, `date` ) AS `delta`, #old := `date` AS `old`
FROM `life`
ORDER BY `date`DESC ) query1
GROUP BY `delta`
ORDER BY `delta`
When selecting a DATE and that date does not exist in my table it currently will return an empty result set. How can I be able to return the number zero for those empty result sets instead?:
SELECT SUM(TOTAL), SUM(5STAR), STORE, DATE
FROM `table` WHERE DATE >= '2012-02-24' GROUP BY TOTAL
MySQL returned an empty result set (i.e. zero rows)
I want to instead return the results of the SUM(TOTAL) and SUM(5STAR) (if zero rows) as the number zero (0).
FULL TABLE STRUCTURE:
ID = Primary
DATE = UNIQUE (date)
STORE
5STAR
4STAR
3STAR
2STAR
1STAR
TOTAL
FROM = UNIQUE
Try COALESCE
SELECT COALESCE(SUM(TOTAL),0), COALESCE(SUM(5STAR),0), STORE, DATE
FROM `table` WHERE DATE >= '2012-02-24' GROUP BY TOTAL
TRY
SELECT
IFNULL(SUM(TOTAL), 0) AS total,
IFNULL(SUM(5STAR), 0) AS FiveStar,
STORE,
DATE
FROM `table`
WHERE DATE >= '2012-02-24'
GROUP BY TOTAL
Reference
I think it would be easier to handle the empty result set on the PHP side (count the returned rows). If you want to handle it in the database, you should create a stored procedure.
DELIMITER $$
CREATE PROCEDURE `mc`.`new_routine` (IN DT DATETIME)
BEGIN
IF EXISTS (SELECT 1 FROM `table` WHERE DATE >= #DT)
THEN
SELECT SUM(TOTAL) AS SumTotal, SUM(5STAR) AS Sum5Star, STORE, `DATE`
FROM `table`
WHERE DATE >= #DT
GROUP BY TOTAL;
ELSE
SELECT 0 AS SumTotal, 0 AS Sum5Star, NULL AS STORE, NULL AS `DATE`;
END IF;
END
Using SQL Server 2008, I want to calculate the timespan, in seconds, that has occurred between two times.
The start date, is the timestamp of the last occurance of where a specific ID exists (if the filter is true), get the time from that timestamp record, and do a DATEDIFF() against the current processing time and return a value, #LastEventTimespan, in seconds.
DECLARE #CurrentProcessTime DATETIME
DECLARE #LastEventTimespan DATETIME
SET #CurrentProcessTime = GetDate()
-- find the timespan since the last session event
-- DATEDIFF ( datepart , startdate , enddate )
SELECT MAX(PageVisitEventID) AS LastPageVisitEventID, #LastEventTimespan = DATEDIFF(second , DateAdded , #CurrentProcessTime )
FROM PageVisitEvents
WHERE UserID = #UserID
GROUP BY LastPageVisitEventID
I figured I could get the MAX ID of the filter and process accordingly but am unable to set the #LastEventTimespan, however trying to assign a value when doing data-retrieval is a no-no.
How can I get around this?
Thanks.
I guess you want something like this.
DECLARE #LastEventTimespan INT
SELECT TOP 1 #LastEventTimespan = DATEDIFF(SECOND, DateAdded, GETDATE())
FROM PageVisitEvents
WHERE UserID = #UserID
ORDER BY PageVisitEventID DESC
This will calculate the difference in seconds between DateAdded for the highest value of PageVisitEventID for a given user and the current DateTime. I changed the data type of #LastEventTimespan to INT because it probably makes more sense when dealing with seconds.
You can replace the SELECT statement with this one:
SELECT TOP 1
#LastEventTimespan = DATEDIFF(second , DateAdded , #CurrentProcessTime )
FROM PageVisitEvents
WHERE UserID = #UserID
ORDER BY PageVisitEventID desc
I've done such queries many times and never had problems.