I need to write a stored procedure whereby I must find the total time a song has been played on the radio in the last 6 months for each month, so 6 outputs, this month, last month, and so on until 6 months ago. To do this I must used a Track_no's start_airtime & finish_airtime. The total of these will give me the total air time and must be separated by each individual month, and the output should contain two coloumns, firstly the month name, and secondly the total time it has been aired.
Note the start air time and finish air time is the length of the song, but i don't have this information available to use to must derive it from these two values.
My table set up is as follows:
track_no TINYINT(3) UNSIGNED
genre VARCHAR(60)
purchasedate DATE NOT NULL
start_airtime TIME NOT NULL
finish_airtime TIME
artist CHAR(9)
I am completely clueless about stored procedures so any information at all would be grateful. Many thanks.
so far my code is :
DELIMITER //
DROP PROCEDURE IF EXISTS airtime_of_month//
CREATE PROCEDURE airtime_of_month(IN track_input TINYINT)
BEGIN
DECLARE x, track_num TINYINT;
DECLARE totalTime VARCHAR(25);
SET x = 7;
SET track_num = track_input;
WHILE x > 1 DO
SET x = x - 1;
SELECT track_no INTO track_num
FROM Radio
WHERE track_input = track_no;
IF (track_input IS NULL) THEN
SELECT CONCAT(track_input, ' Is Not
A Valid Track Number') as _;
ELSE
SELECT track_no,
CONCAT(FLOOR(HOUR(TIMEDIFF(finish_airtime,start_airtime)) / 24), ' days ',
MOD(HOUR(TIMEDIFF(finish_time,start_airtime)), 24), ' hrs ',
MINUTE(TIMEDIFF(finish_airtime,start_airtime)), ' minutes ') AS total_AirTime
FROM Radio
WHERE DATE_SUB(MONTH(purchasedate), INTERVAL x
MONTH);
END IF;
SELECT track_num;
END WHILE;
END //
CALL airtime_of_month (2)/
DELIMITER ;
However when i do this it does compile but doesn't return anything. Any ideas?