I am using MySQL to make a report showing the number of hours billed for a particular date range and project. The complexity is that the date range is variable for each project (different start month and start day). This information is coming from a value in another database/table.
I have the following UDF in MySQL:
DELIMITER //
CREATE FUNCTION TimeLeft(startday INT, today INT) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s INT;
IF startday < today THEN SET s = 0;
ELSE SET s = 1;
END IF;
RETURN s;
END //
DELIMITER;
I use that function in the following query, which is supposed to take the value returned in the TimeLeft function to determine the values for the start month (month(curdate())-@xx) and start day (@yy) for each project to calculate the hours:
AND time_records.record_date >= concat('2012/', month(curdate())-@xx , '/' , @yy)
Here's how I am setting the values for @xx and @yy:
SET @xx = 0; #this is the value that we will use to manipulate the month for the date range
SET @yy = 0;
@yy:= SELECT start_day_of_month FROM dashboard.client; #this doesn't seem to work
SELECT @xx:= TimeLeft(@yy,dayofmonth(curdate()));
I am getting some issues:
- @yy is not getting the value - possibly my syntax is wrong?
- The variables are set at the top of the code, so they are not getting changed for each project as they should be (there should be a different @xx and @yy for each project since each one has a different start and end date).
Here's the full query:
#below is where I assign the variables
SET @xx = 0; #this is the value that we will use to manipulate the month for the date range
SET @yy = 0;
@yy:= SELECT start_day_of_month FROM dashboard.client; #this doesn't seem to work
SELECT @xx:= TimeLeft(@yy,dayofmonth(curdate()));
# below is the MySQL query that is meant to use the variables assigned above
SELECT X.expr1 AS 'Project Name', @monthly_hours - SUM(X.expr2) AS 'Hours Billed
FROM
(SELECT
projects.name AS expr1
, sum(time_records.value) AS expr2
FROM project_objects
INNER JOIN projects
ON projects.id = project_objects.project_id
INNER JOIN time_records
ON time_records.parent_id = project_objects.id
WHERE time_records.parent_type = 'Task'
AND time_records.record_date >= concat('2012/', month(curdate())-@xx , '/' , @yy)
AND time_records.record_date <= curdate()
GROUP BY projects.name
UNION
SELECT
projects.name AS expr1
, sum(time_records.value) as expr2
FROM projects
INNER JOIN time_records
ON projects.id = time_records.parent_id
WHERE time_records.parent_type = 'Project'
AND time_records.record_date >= concat('2012/', month(curdate())-@xx , '/' , @yy)
AND time_records.record_date <= curdate()
GROUP BY projects.name) X
GROUP BY X.expr1
I think there is some issue of where I am assigning the variables @xx and @yy. These should be done for each individual Project, so putting them up on the top is probably not the best idea. I'm also not sure if I am assigning the @yy value correctly. It's supposed to query the value of the field of a table that is in another database but it keeps throwing a syntax error on the @yy assignment to that field.