Getting a minute interval between start and end time mysql - mysql

Id | starttime | endtime |
1 | 1999-05-07 15:00 | 1999-05-07 16:45 |
How do I get it interval of 1 minute from the start time to the end time.
So I want something like this 15:01, 15:02.
Please any help will be appreciated new to sql and trying to learn it as best I can. Thanks
NOTE :
I don't want the whole minute. I want to get every minute from start to end I need it to count the users that logged in every minute of the game if that makes any sense

SELECT *,TIMESTAMPDIFF(MINUTE,starttime,endtime) FROM Table1
Create this function
CREATE function getTimeAll(id1 int)
RETURNS VARCHAR(65535) DETERMINISTIC
BEGIN
DECLARE time1 varchar(65535);
DECLARE intrvlMin int;
DECLARE c int;
SET intrvlMin=(SELECT TIMESTAMPDIFF(MINUTE,starttime,endtime) FROM Table1 WHERE id = id1);
SET c=1;
SET time1 = '';
While c < intrvlMin
do
SET time1=CONCAT(time1,(SELECT date_format(date_add(starttime,interval c minute),'%H:%i') FROM Table1 WHERE id = id1),',');
SET c = c + 1;
end WHILE;
SET time1=CONCAT(time1,(SELECT date_format(date_add(starttime,interval c minute),'%H:%i') FROM Table1 WHERE id = id1));
RETURN time1;
END ;
Then Run
SELECT `Id`, `starttime`, `endtime`,getTimeAll(`Id`) AS Times FROM Table1;
output
Id starttime endtime Times
1 1999-05-07 15:00:00 1999-05-07 16:45:00 15:01,15:02,15:03,15:04,15:05,15:06,15:07,15:08,15:09,15:10,15:11,15:12,15:13,15:14,15:15,15:16,15:17,15:18,15:19,15:20,15:21,15:22,15:23,15:24,15:25,15:26,15:27,15:28,15:29,15:30,15:31,15:32,15:33,15:34,15:35,15:36,15:37,15:38,15:39,15:40,15:41,15:42,15:43,15:44,15:45,15:46,15:47,15:48,15:49,15:50,15:51,15:52,15:53,15:54,15:55,15:56,15:57,15:58,15:59,16:00,16:01,16:02,16:03,16:04,16:05,16:06,16:07,16:08,16:09,16:10,16:11,16:12,16:13,16:14,16:15,16:16,16:17,16:18,16:19,16:20,16:21,16:22,16:23,16:24,16:25,16:26,16:27,16:28,16:29,16:30,16:31,16:32,16:33,16:34,16:35,16:36,16:37,16:38,16:39,16:40,16:41,16:42,16:43,16:44,16:45
DEMO
https://www.db-fiddle.com/f/nge35TSTWyd3pLSw6X1TE4/2

You might need to use loop ,
Firstly
select TIMESTAMPDIFF(minute,starttime,endtime) from table1
Here you will get 105
This calculates number of minutes between 2 times
SELECT date_format(date_add(starttime,interval 1 minute),'%H:%i') from table1
SELECT date_format(date_add(starttime,interval 2 minute),'%H:%i') from table1
SELECT date_format(date_add(starttime,interval 3 minute),'%H:%i') from table1
loop this till 105 times with increment interval time

Related

MySQL) How to remove duplicated rows working with WHILE loop?

I have a table called cart and an empty table sort in database. My goal is to transmit & split values in comma spread from one table to another like this:
(table cart)
id | food
---+--------------
1 | Carrots, Cucumbers
2 | Dandelions
3 | Salmons
4 | Cucumbers, Potatoes
5 | Tomatoes
(table sort after run query)
id | food
---+--------------
1 | Carrots
1 | Cucumbers
2 | Dandelions
3 | Salmons
4 | Cucumbers
4 | Potatoes
5 | Tomatoes
An issue is, it seems like the keywords DISTINCT, GROUP BY and ORDER BY won't work properly inside of the WHILE clause. When I run my query, MySQL inserts a whole list of values multiple times based on a total of id count.
Have a look at this query:
DELIMITER //
CREATE FUNCTION fx_splitString(columnName TEXT, pos INT)
RETURNS TEXT
RETURN TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(columnName, ',', pos), ',', -1)) //
CREATE PROCEDURE pd_splitRow()
BEGIN
DECLARE rowToll INT;
SET rowToll = (SELECT COUNT(id) FROM cart);
SET #outerCount = 1;
WHILE #outerCount <= rowToll DO
INSERT INTO sort (id, food)
SELECT DISTINCT id, fx_splitString(food, #stringToll) FROM cart;
SET #outerCount = #outerCount + 1;
END WHILE;
END //
DELIMITER ;
CALL pd_splitRow();
Now I'm trying to find other ways to accomplish this but none of these work.
Try 1: Nested Loop
This idea is aborted because MySQL won't allow to set multiple rows. I can't use to create variable
Error Code: 1242. Subquery returns more than 1 row 0.016 sec
DECLARE rowToll INT;
SET rowToll = (SELECT COUNT(id) FROM cart);
SET #stringToll = (SELECT LENGTH(food) - LENGTH(REPLACE(food, ',', '')) + 1 FROM cart); /* error */
SET #outerCount = 1;
SET #innerCount = 1;
WHILE #outerCount <= rowToll DO
WHILE #innerCount <= #stringToll DO
INSERT INTO sort (id, food)
SELECT DISTINCT id, fx_splitString(food, #stringToll) FROM cart;
SET #innerCount = #innerCount + 1;
END WHILE;
SET #outerCount = #outerCount + 1;
END WHILE;
Try 2: Self Join
This one simply does not insert any of values from cart table.
WHILE #begin <= rowCount DO
INSERT INTO sort (id, food)
SELECT DISTINCT cte1.id, fx_splitString(cte1.food, #begin) FROM cart cte1
INNER JOIN cart cte2 ON cte1.id = cte2.id AND cte1.food = cte2.food;
SET #begin = #begin + 1;
END WHILE;
I could just use in the examples from the other guys' query but I want to create my own for improving skills.
Are there any ways to group the rows working with WHILE loop?

how to splite two dates between week in mysql month wise

I want to display number of row in MySQL according number of week.
Suppose we pass jan startdate 2017-1-1 and endDate 2017-1-31 then totdal days is 31. We have to divide by 7 then it will 4 week and 3 days so total 5 week so I have to display like this
for jan
WeekRow
1
2
3
4
5
for Feb
WeekRow
1
2
3
4
I am trying to apply a query, but I am unable to do it.
I am facing this issue from long time, but still unable to fix.
below code is working to get rid your problem.
i have wrote this code in hurry .....later on i ll optimize this ...
have a look..and let me know if you got any problem to understand it...
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_month_days_split`(in in_start_date datetime , in in_end_date datetime)
BEGIN
declare temp_date datetime;
declare temp_date_data int ;
drop temporary table if exists temp_split;
create temporary table temp_split
(
number bigint not null
);
while(in_start_date <= in_end_date)
do
set temp_date = (select date_add(in_start_date,interval 7 day));
set temp_date_data = (select day(date_add(in_start_date,interval 6 day)));
set #temp_last_number = (select number from temp_split order by number desc limit 1);
if(temp_date_data % 7) =0
Then
insert into temp_split(number)
select (temp_date_data/7);
else
insert into temp_split(number)
select #temp_last_number+1;
end if ;
set in_start_date = temp_date ;
end while;
select * from temp_split;
END
call
call sp_month_days_split('2017-12-01', '2017-12-31');

Select Inside Insert Statement - MySQL Cursors

I tried some, but I couldn't find the solution, somehow I managed to get this result.
Here is the query:
DELIMITER ##
CREATE PROCEDURE test1(start_date DATE,end_date DATE)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE present INT;
DECLARE total INT;
-- Declare the cursor
DECLARE id CURSOR
FOR
SELECT staff_id FROM ost_staff;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- Open the cursor
DROP TEMPORARY TABLE IF EXISTS reports;
CREATE TEMPORARY TABLE IF NOT EXISTS reports
(
staff_id INT(10),
present INT(10),
total INT(10)
);
OPEN id;
read_loop: LOOP
FETCH id INTO a;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO reports(staff_id,present,total)
SELECT (COUNT(I.interval_start)) AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1 DAY),start_date) AS total
FROM effort_frequency E
RIGHT OUTER JOIN time_intervals I ON I.interval_start = E.log_date
AND E.staffid=a AND E.log_date BETWEEN start_date AND end_date
LEFT OUTER JOIN ost_holidays H ON H.holiday_date = I.interval_start
WHERE DATE_FORMAT(I.interval_start,'%a') = 'Sun' OR H.holiday_date = I.interval_start OR E.total_effortspent IS NOT NULL;
-- Close the cursor
END LOOP;
CLOSE id;
END ##
I got the below result:
+----------+-----------------+
| staff_id | present | total |
+----------+---------+-------+
| (NULL) | 23 | 24 |
| (NULL) | 22 | 24 |
+----------+---------+-------+
I'm getting (NULL) for staff_id, How can I get the staff_id's there ?
I tried using declared variable 'a' in insert statement, but at that time I got only staff_id, I didn't get the other 2 fields, I can't get the staff_id from the select inside insert statement coz there is some problem.
Now what i need is I need to insert the staff_id from the variable 'a' into that temporary table.
note: I'm really new to this stored procedure, but somehow managed till here, Its good if I get some detail on how to use the Select inside Insert including the solution for this.
Try this -
SELECT a, (COUNT(I.interval_start)) AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1 DAY),start_date) AS total
Your INSERT requires three fields, but your SELECT statement only selects two: present and total .
Try:
SELECT E.staffid, (COUNT(I.interval_start))
AS present, DATEDIFF(DATE_ADD(end_date,INTERVAL 1 DAY),start_date) AS total

MySQL add timestamp values

I have a table in MySQL with this format: (time = timestamp on insert)
id | tid | uid | time
31 | 1 | 14 | 2011-05-19 05:42:37 //start timestamp)
41 | 1 | 14 | 2011-05-19 07:18:42 //stop timestamp)
45 | 1 | 14 | 2011-05-19 07:18:49 //start timestamp)
46 | 1 | 14 | 2011-05-19 07:28:42 //stop timestamp)
What I need is to make a select that adds the time differences like this
(41 - 31) + (46 - 45) (i'm using the id's instead of the actual time values to better understand what I need to do )
something like SELECT (something that does this) AS TotalTimeSpent WHERE tid = '1'
If you insist on using this table layout (and I really hope you change your mind, it is truly horrific), you can do it with cursors in a stored procedure.
Pseudo-code would be something like this:
CREATE PROCEDURE gettotaltime()
BEGIN
DECLARE total, curr, prev DATETIME;
DECLARE odd INT DEFAULT 1;
DECLARE done INT DEFAULT 0;
DECLARE c CURSOR FOR SELECT time FROM tbl;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN c;
read_loop: LOOP
FETCH c INTO curr;
IF odd=0 THEN
SET total=dateadd(total,datediff(curr,prev)); -- or something similar, I forget
END IF;
SET prev=curr;
SET odd=1-odd;
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE c;
SELECT total;
END;

MySql: Count amount of times the words occur in a column

For instance, if I have data in a column like this
data
I love book
I love apple
I love book
I hate apple
I hate apple
How can I get result like this
I = 5
love = 3
hate = 2
book = 2
apple = 3
Can we achieve this with MySQL?
Here is a solution only using a query:
SELECT SUM(total_count) as total, value
FROM (
SELECT count(*) AS total_count, REPLACE(REPLACE(REPLACE(x.value,'?',''),'.',''),'!','') as value
FROM (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(t.sentence, ' ', n.n), ' ', -1) value
FROM table_name t CROSS JOIN
(
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n
) n
WHERE n.n <= 1 + (LENGTH(t.sentence) - LENGTH(REPLACE(t.sentence, ' ', '')))
ORDER BY value
) AS x
GROUP BY x.value
) AS y
GROUP BY value
Here is the full working fiddle: http://sqlfiddle.com/#!2/17481a/1
First we do a query to extract all words as explained here by #peterm(follow his instructions if you want to customize the total number of words processed). Then we convert that into a sub-query and then we COUNT and GROUP BY the value of each word, and then make another query on top of that to GROUP BY not grouped words cases where accompanied signs might be present. ie: hello = hello! with a REPLACE
If you want to perform such kind of text analysis, I would recommend using something like lucene, to get the termcount for each term in the document.
This query is going to take a long time to run if your table is of any decent size. It may be better to keep track of the counts in a separate table and update that table as values are inserted or, if real time results are not necessary, to only run this query every so often to update the counts table and pull your data from it. That way, you're not spending minutes to get data from this complex query.
Here's what I've for you so far. It's a good start. The only thing you need to do is modify it to iterate through the words in each row. You could use a cursor or a subquery.
Create test table:
create table tbl(str varchar(100) );
insert into tbl values('data');
insert into tbl values('I love book');
insert into tbl values('I love apple');
insert into tbl values('I love book');
insert into tbl values('I hate apple');
insert into tbl values('I hate apple');
Pull data from test table:
SELECT DISTINCT str AS Word, COUNT(str) AS Frequency FROM tbl GROUP BY str;
create a user defined function like this and use it in your query
DELIMITER $$
CREATE FUNCTION `getCount`(myStr VARCHAR(1000), myword VARCHAR(100))
RETURNS INT
BEGIN
DECLARE cnt INT DEFAULT 0;
DECLARE result INT DEFAULT 1;
WHILE (result > 0) DO
SET result = INSTR(myStr, myword);
IF(result > 0) THEN
SET cnt = cnt + 1;
SET myStr = SUBSTRING(myStr, result + LENGTH(myword));
END IF;
END WHILE;
RETURN cnt;
END$$
DELIMITER ;
Hope it helps
Refer This
Split-string procedure is not my job. You can find it here
http://forge.mysql.com/tools/tool.php?id=4
I wrote you the rest of code.
drop table if exists mytable;
create table mytable (
id int not null auto_increment primary key,
mytext varchar(1000)
) engine = myisam;
insert into mytable (mytext)
values ('I love book,but book sucks!What do you,think about it? me too'),('I love apple! it rulez.,No, it sucks a lot!!!'),('I love book'),('I hate apple!!! Me too.,!'),('I hate apple');
drop table if exists mywords;
create table mywords (
id int not null auto_increment primary key,
word varchar(50)
) engine = myisam;
delimiter //
drop procedure if exists split_string //
create procedure split_string (
in input text
, in `delimiter` varchar(10)
)
sql security invoker
begin
declare cur_position int default 1 ;
declare remainder text;
declare cur_string varchar(1000);
declare delimiter_length tinyint unsigned;
drop temporary table if exists SplitValues;
create temporary table SplitValues (
value varchar(1000) not null
) engine=myisam;
set remainder = input;
set delimiter_length = char_length(delimiter);
while char_length(remainder) > 0 and cur_position > 0 do
set cur_position = instr(remainder, `delimiter`);
if cur_position = 0 then
set cur_string = remainder;
else
set cur_string = left(remainder, cur_position - 1);
end if;
if trim(cur_string) != '' then
insert into SplitValues values (cur_string);
end if;
set remainder = substring(remainder, cur_position + delimiter_length);
end while;
end //
delimiter ;
delimiter //
drop procedure if exists single_words//
create procedure single_words()
begin
declare finish int default 0;
declare str varchar(200);
declare cur_table cursor for select replace(replace(replace(replace(mytext,'!',' '),',',' '),'.',' '),'?',' ') from mytable;
declare continue handler for not found set finish = 1;
truncate table mywords;
open cur_table;
my_loop:loop
fetch cur_table into str;
if finish = 1 then
leave my_loop;
end if;
call split_string(str,' ');
insert into mywords (word) select * from splitvalues;
end loop;
close cur_table;
end;//
delimiter ;
call single_words();
select word,count(*) as word_count
from mywords
group by word;
+-------+------------+
| word | word_count |
+-------+------------+
| a | 1 |
| about | 1 |
| apple | 3 |
| book | 3 |
| but | 1 |
| do | 1 |
| hate | 2 |
| I | 5 |
| it | 3 |
| lot | 1 |
| love | 3 |
| me | 2 |
| No | 1 |
| rulez | 1 |
| sucks | 2 |
| think | 1 |
| too | 2 |
| What | 1 |
| you | 1 |
+-------+------------+
19 rows in set (0.00 sec)
The code must be improved in order to consider any punctuation but this is the general idea.