I have 2 stored Procedures in MySQL. I want to call one from the other but I need some help with the syntax
Here is Procedure 1 FindPreviousDate
DELIMITER $$ CREATE PROCEDURE `FindPreviousDate`(IN eventdate DATETIME, IN lookbackDays INT, IN symbol VARCHAR(20))
BEGIN
SELECT *
FROM Price a
WHERE a.eventDate between (eventDate - interval lookbackDays day) and (eventdate - interval 1 day) and a.symbol = symbol
ORDER BY a.eventDate DESC
LIMIT 1;
END
Here is Procedure 2 FindCloseEvent
CREATE PROCEDURE `FindCloseEvent`(IN startdate DATETIME, IN enddate DATETIME,IN symbol VARCHAR(20), IN cutoff DOUBLE)
BEGIN
SELECT *
FROM Price a
WHERE a.eventDate between startdate and enddate and
(SELECT COUNT(*) from Price b where b.eventDate = a.eventDate and b.closePrice < cutoff and a.symbol = b.symbol and
(SELECT COUNT(*) from Price c where c.eventDate = (b.eventDate - interval 1 day) and c.closePrice >= cutoff and b.symbol = c.symbol));
END
I want to replace this code in Procedure 2 with the result of Procedure 1
(b.eventDate - interval 1 day)
I need help getting the syntax right. I'm not even sure if mySql allows for what I'm asking.
Thanks!
On procedures you can use OUT variables, which get filled and are available outside the procedure.
OR
Since a MySQL procedure does not return a value. You'll need a stored function to achieve this.
use the example given into below link to call a function1 from your procedure2
http://www.java2s.com/Code/SQL/Procedure-Function/Callanotherfunction.htm
you can even store the result of function1 into some variable of procedure2 and use it later in query
Related
I have a query :
insert into fookoo_business
select stat_date, sum(spend), sum(revenue)
from hooloo_business;
that i want to run for each date from '2017-01-20' until yesterday (it means the query will run 434 times if we're at 01/04/2018), for each date separately
(in a loop).
how can i create a loop in Mysql to do it for me?
I have tried:
creating procedure for the query select #stat_date, sum(spend), sum(revenue)
I called 'query'
then :
CREATE PROCEDURE loop_procedure()
BEGIN
SET #stat_date='2018-03-20';
CALL 'query';
REPEAT
SET #stat_date = #stat_date + INTERVAL 1 DAY;
UNTIL #stat_date = CURDATE() END REPEAT;
END
eventually i've used the following logic within a stored procedure to fetch the data:
PROCEDURE `x_monitoring_loop`()
BEGIN
DECLARE i INT;
DECLARE len INT;
SET len = 434;
SET i = 0;
WHILE (i < len) DO
SET #stat_date= CURDATE()-INTERVAL 1 DAY;
SET #stat_date= #stat_date- INTERVAL i DAY;
Insert query;
SET i = i +1;
END WHILE;
This way the query ran 434 times for each day, beginning at current date - 1 day.
I do not know why you want to use a procedure,I think we can just use a query sql to do it:
INSERT INTO fookoo_business
SELECT stat_date, SUM(spend), SUM(revenue)
FROM hooloo_business
WHERE stat_date BETWEEN STR_TO_DATE('2017-01-02', '%Y-%m-%d') -- start date
AND DATE_SUB(NOW(), INTERVAL 1 DAY) -- end date
GROUP BY stat_date;
I am trying to pull a sum from my data that is comparing one month to the next. The below code provides what I need to compare two months but I need to run this over 4 years.
SELECT as_of_date,
Sum(case when `as_of_date` = '2014-02-01' AND disclosure_number IN(
SELECT disclosure_number
FROM `gnma_cohort`
WHERE obp BETWEEN '125001' AND '150000' AND in_the_money BETWEEN '0'
AND '49.999' AND `as_of_date` = '2014-01-01') THEN 1 END) AS '2014-
02-01'
FROM `gnma_cohort`
I am new to MySQL but from what I have read it seems a While loop would be my best option. Below is my attempt at creating the loop but I keep getting errors on my delimiter, is this not the correct format for mysql in PHPMYAdmin? I also do not have the standard ; delimiter in my code that I have seen other examples use, but I never use the ; delimiter in phpmyadmin. Any advice on how to fix my code would be greatly appreciated, thank you!
DELIMITER $$
DROP PROCEDURE IF EXISTS itmdates$$
CREATE PROCEDURE itmdates()
BEGIN
DECLARE startdate DATE
WHILE startdate < '2017-10-01' DO
SELECT as_of_date,
Sum(case when `as_of_date` = startdate AND disclosure_number
IN(
SELECT disclosure_number
FROM `gnma_cohort`
WHERE obp BETWEEN '125001' AND '150000' AND in_the_money
BETWEEN '0' AND '49.999' AND `as_of_date` =
DATE_SUB(startdate, INTERVAL 1 MONTH) THEN 1 END) AS
'startdate'
SET startdate = DATE_ADD(startdate, INTERVAL 1 MONTH)
END WHILE
END$$
I want to assign value of a query to a datetime variable in MySql Stored Procedure.
I'm trying this -
DECLARE myDate DATETIME;
SET myDate = (SELECT date1 FROM myTable WHERE Id = var_myId);
And this
DECLARE myDate DATETIME;
SELECT date1 into myDate FROM myTable WHERE Id = var_myId;
Both don't seem to work as I am not getting the desired result after running the proc.
EDIT
Problem is in this statement -
initial_Date DATETIME; -- param1
interval INTEGER; -- param2
SET var_date2 = DATE_ADD(initial_Date , INTERVAL interval MINUTE);
When I select var_date2 I get null as result.
I find two issue with your stored procedure.
First, you are naming a variable using an sql key word interval. You should rename with an word which is not an sql key word.
The other is you are not setting an out put variable. You can see the MySQL tutorials on stored procedure.
You can use and try the code below:
delimiter //
create procedure test(in initial_time datetime, in minuteInterval integer(2),
out final_time datetime)
begin
set final_time = date_add(initial_time, interval minuteInterval minute);
end//
delimiter ;
For testing you can try:
call test(now(), 60, #final_time);
select #final_time;
Here is the screenshot of my test with mysql 5.5.21:
I would like to change the interval in this SQL statement, based on a parameter in a stored procedure. I want to use three different intervals: 1 day, 8 hours, 1 hour
CREATE DEFINER= 'dbshizzle' PROCEDURE `getData`(in sD text(17), in sT text(8))
BEGIN
select stime, sval
from tblNumber
where sDix = 'allright'
and timestamp >= now() - interval 1 day
order by timestamp;
END
Should I use an IF statement with an integer parameter, or a text parameter?
How about just adjusting the parameter and passing in the value as hours?
CREATE DEFINER = 'dbshizzle' PROCEDURE `getData`(
in in_sD text(17), -- should change to varchar
in in_sT text(8), -- should change to varchar
in in_hours int
)
BEGIN
select stime, sval
from tblNumber
where sDix = 'allright'
and timestamp >= now() - interval in_hours hour
order by timestamp;
END;
In the below procedure,
It gives error ([Err] 1111 - Invalid use of group function) when I use WHERE statements and it gives another error ([Err] 1054 - Unknown column 'roomid' in 'having clause') when I use HAVING statements instead of WHERE, although I have the roomid column defined in the table.
Does anybody have an idea on how can I fix this issue? The code was working perfectly when I tried under another mySQL version I guess.
DELIMITER //
DROP PROCEDURE IF EXISTS `findAVG`//
CREATE DEFINER=`kamer`#`%` PROCEDURE `findAVG`(IN rid INT, startDate DATETIME, OUT Score DOUBLE)
BEGIN
DECLARE finishDate DATETIME;
DECLARE DateIterator DATETIME;
DECLARE avgPrice DOUBLE;
SET avgPrice = 0;
SET Score = 0;
SELECT price into Score
FROM bookings
WHERE roomid = rid
ORDER BY ABS( DATEDIFF( bookings.date, startDate)) LIMIT 1;
IF (Score = 0) THEN
SET DateIterator = startDate;
SET finishDATE = DATE_ADD(startDate, INTERVAL 30 DAY);
WHILE DateIterator <= finishDate DO
SELECT price
INTO avgPrice
FROM prices
WHERE roomid = rid
AND date = DateIterator
ORDER BY DATEDIFF(startDate, prices.timestamp) LIMIT 1;
SET DateIterator = DATE_ADD(DateIterator, INTERVAL 1 DAY);
END WHILE;
SET Score = AVG(avgPrice);
END IF;
UPDATE bookings SET price=Score WHERE roomid= rid AND date=startDate;
END
"Invalid use of group function" mean you've used a column in the group by function that does not appear in the select clause or the inverse.
i.e. :
SELECT A,B,C,COUNT(*) FROM mytable GROUP BY A,B,C
Grouping is done on the 3 firts's columns and the count is done on each line where A,B and C are similar.
For example, if you try to pass "GROUP BY A,B" SQL wont know what to do with C column...
For HAVING problem, it seem to me the HAVING clause is applicated after the select have been done.
So if you're using a column that doesn't appear in the SELECT clause, then it raise an error.