Ok. I'm about to give up on this one.
When I run the following query, I get "Error Code: 1305. FUNCTION statpurchase.playerId does not exist". I don't get a line number, but I strongly suspect the COUNT(DISTINCT(WHEN clauses.
The query is attempting to compute the percent of unique playerIds who make a purchase in a given day for a range of time. statpurchase.playerId is a valid column name.
It's a poor man who blames his tools, but I suspect its possibly a parser error similar to this one.
delimiter $$
CREATE PROCEDURE `percentUniquesPurchasing`(in startTime datetime, in endTime datetime, in placeId int)
BEGIN
declare total_uniques int;
declare iOS_uniques int;
declare desktop_uniques int;
declare i datetime;
set i = startTime;
CREATE TEMPORARY TABLE results (
theday datetime,
total float,
iOS float,
desktop float
);
while(i < endTime + INTERVAL 1 DAY) do
select count(distinct(statplaysession.playerId)),
count(distinct(case when touchInterface = 1 then statplaysession.playerId else null end)),
count(distinct(case when touchInterface = 0 then statplaysession.playerId else null end))
into
total_uniques, iOS_uniques, desktop_uniques
from rbxstats.statplaysession
where
statplaysession.start > i and
statplaysession.start < i + INTERVAL 1 DAY and
statplaysession.placeId = placeId;
insert into results (theday, total, iOS, desktop)
select i,
if(total_uniques > 0, count(distinct(statpurchase.playerId)) / total_uniques, 0),
if(iOS_uniques > 0, count(distinct(statpurchase.playerId(case when touchInterface = 1 then statpurchase.playerId end))) / iOS_uniques, 0),
if(desktop_uniques > 0, count(distinct(statpurchase.playerId(case when touchInterface = 0 then statpurchase.playerId end))) / desktop_uniques,0)
from rbxstats.statpurchase where
statpurchase.timestamp > i and
statpurchase.timestamp < i + INTERVAL 1 DAY and
statpurchase.placeId = placeId;
set i = i + INTERVAL 1 DAY;
end while;
select * from results;
drop temporary table results;
END$$
on these 2 lines you try to use statpurchase.playerId as a function, which it doesnt seem to be, its a column in your table
if(iOS_uniques > 0, count(distinct(statpurchase.playerId(case when
if(desktop_uniques > 0, count(distinct(statpurchase.playerId(case when
Related
This question already has answers here:
MySQL function to find the number of working days between two dates
(39 answers)
Closed 4 years ago.
How do I return the number of days between two dates minus the number of saturday and sundays on that period ?
The DATEDIFF function on mysql gives me the number of days but doesnt exclude the weekends.
Example, I have two columns and want to add a third one that is the differente between the two.
I would appreciate any help.
Try using this
Declare #Date1 Datetime, #Date2 Datetime;
Select #Date1 = '8/1/2018', #Date2 = '8/7/2018'
Select Datediff(dd, #Date1 , #Date2) - (Datediff(wk, #Date1 , #Date2) * 2) -
Case When Datepart(dw, #Date1) = 1 Then 1 Else 0 End +
Case When Datepart(dw, #Date2) = 1 Then 1 Else 0 End
DROP FUNCTION IF EXISTS GetNumberOfDays;
CREATE FUNCTION GetNumberOfDays(P_Date1 varchar(20), P_Date2 varchar(20))
RETURNS int(100)
BEGIN
DECLARE x INT;
DECLARE V_Date1 date;
DECLARE V_Date2 date;
SET V_Date1 := STR_TO_DATE(P_Date1, '%d/%m/%Y');
SET V_Date2 := STR_TO_DATE(P_Date2, '%d/%m/%Y');
SET x := 0;
WHILE V_Date1 <= V_Date2
DO
IF (DAYOFWEEK(V_Date1) != 7 AND DAYOFWEEK(V_Date1) != 1)
THEN
BEGIN
SET x = x + 1;
END;
END IF;
SET V_Date1 := DATE_ADD(V_Date1, INTERVAL 1 DAY);
END WHILE;
RETURN x;
END;
SELECT GetNumberOfDays('01/08/2018', '11/08/2018')
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 developing a Java application using MySQL. I need to know which is the week of each month, of the stored dates. Is there any MySQL function for that ? Basically , if i was to use this for the current date (13.09) it would show me its in week number 2 and tomorrow it will be week number 3.
You can play with the WEEK() function, and see if it suits your needs. Here I'm using WEEK(date, 3) that will return the week of the year from 1 to 53, starting from Mondays:
set #my_date = '2015-09-13';
SELECT
WEEK(#my_date, 3) -
WEEK(#my_date - INTERVAL DAY(#my_date)-1 DAY, 3) + 1
AS week_number;
WEEK(date, 3) will return the week of the year of the selected date
WEEK(date - INTERVAL DAY(#my_date)-1 DAY, 3) will return the week of the year of the first day of the month of the selected date
It will return 1 for 01-March-2015 (because it's the first day of the month so it's week 1) and 2 for 02-March-2015 (because weeks starts from Mondays, so it's a new week). If this is not the desidered behaviour you should specify your requirements more precisely.
Please see a fiddle here.
Unfortunately, there isn't a "weekofmonth" function, but you could use dayofmonth, and manipulate the result a bit:
SELECT CURRENT_DATE(),
FLOOR((DAYOFMONTH(CURRENT_DATE()) - 1) / 7) + 1 AS week_of_month
Create a mysql function.
CREATE FUNCTION `WEEK_OF_MONTH`(
datee DATE
) RETURNS INT(11)
BEGIN
DECLARE DayNamee VARCHAR(20);
DECLARE StartDatee DATE;
DECLARE DayNumber INT DEFAULT 0;
SET DayNamee = (SELECT DAYNAME(datee));
SET StartDatee = (SELECT FIRST_DAY(datee));
WHILE StartDatee <= datee DO
IF DayNamee = DAYNAME(StartDatee) THEN
SET DayNumber = DayNumber + 1;
END IF;
SET StartDatee = DATE_ADD( StartDatee, INTERVAL 1 DAY);
END WHILE;
RETURN DayNumber;
END$$
DELIMITER ;
Call as --
SELECT `WEEK_OF_MONTH`('2018-12-31');
Result : 5
I have to find the number of business days using mysql.I am using this query but this is not giving me the correct result
SELECT ((DATEDIFF('2015-05-31', '2015-05-01')) -((WEEK('2015-05-31') - WEEK('2015-05-01')) * 2) -
(case when weekday('2015-05-31') = 6 then 1 else 0 end) - (case when weekday('2015-05-01') = 5 then 1 else 0 end))
as DifD ;
It is giving 19 as output where number of business days should be 20
Somebody please help
Try this!
SET #i=-1;
SELECT SUM(CASE WHEN(WEEKDAY(ADDDATE('2015-05-01', INTERVAL #i:=#i+1 DAY))) < 5 THEN 1 ELSE 0 END) AS `business_days`
FROM `table`
WHERE #i < DATEDIFF('2015-05-31', '2015-05-01');
Hope this answer helps!
drop procedure COUNTWEEKDAYS;
DELIMITER $$
CREATE PROCEDURE COUNTWEEKDAYS (FROMDATE TIMESTAMP, TODATE TIMESTAMP)
begin
declare NOOFWEEKDAYS INTEGER;
set NoOfWeekDays = (datediff(todate, fromdate) + 1)
-((timestampdiff(week, FROMDATE , TODATE) * 2))
-weekday(fromdate)%4
-weekday(todate)%4;
select NOOFWEEKDAYS;
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.