• Welcome to RadioDJ - Free Radio Automation Software Forum. Please log in or sign up.

least times played with SQL?

Started by djdensma, August 14, 2016, 03:39:46 PM

djdensma

how can i apply least times played or least recently played with sql rotation?
I like to use SQL rotation for event music show but same tracks repeats each time.

is there a way around this?

Valdis

It is all about using the right columns in ORDER BY clause, but there might be other things missing from your query.
Please post the SQL query you are using.
I am, therefore I think.
I design and develop web apps, and have created few RadioDJ plugins - https://axellence.lv/downloads/

Filip83

Did you see: http://www.radiodj.ro/community/index.php?topic=6658.0

To not add same artist in the playlist:
AND  `queuelist`.`artist` IS NULL

Least recently played:
ORDER BY `date_played` ASC

Least times played:
ORDER BY `count_played` ASC

you might also want to enforce artist and track repeat rules:
AND ((`start_date` <= Now())
AND (`end_date` >= NOW() OR `end_date` = '2002-01-01 00:00:01'))
AND ((TIMESTAMPDIFF(MINUTE, `date_played`, NOW()) > $TrackRepeatInterval$)
AND (TIMESTAMPDIFF(MINUTE, `artist_played`, NOW()) > $ArtistRepeatInterval$))


But like Valdis said...

www.diskonektedmusic.com
www.soundcloud.com/diskonekted

djdensma

thanks guys.. i will test out recomended order by.. no i didnt sort it that way, will retest and report
also wondering if possible add TTS to event that can read special .xml file only during that event. next event to stop it

thanks

Jhonny

Quote from: djdensma on August 15, 2016, 02:17:10 PM
also wondering if possible add TTS to event that can read special .xml file only during that event. next event to stop it
Yes it can using a trick, with a batch (.bat) file and a small program .bat to .exe for use in a event .
R.T.F.M. means to me, Read The Fantastic Messages.
Einstein says: Make things simple, but don't make simple things simpler! (wise) this is just my life lesson you know.

I don't do drugs ( ͡° ͜ʖ ͡°)
The Radiodj tutorials site is now managed by Domstad radio .nl still  Just in English

Jhonny

R.T.F.M. means to me, Read The Fantastic Messages.
Einstein says: Make things simple, but don't make simple things simpler! (wise) this is just my life lesson you know.

I don't do drugs ( ͡° ͜ʖ ͡°)
The Radiodj tutorials site is now managed by Domstad radio .nl still  Just in English

djdensma

this works. what about if i want to sort by random? how do i sort that?

how can i pull top ten play for particular month?. let say top ten for July 2016

thanks

Valdis

#7
Quote from: djdensma on August 17, 2016, 12:28:06 PM
what about if i want to sort by random? how do i sort that?

ORDER BY RAND()


Quote from: djdensma on August 17, 2016, 12:28:06 PM
how can i pull top ten play for particular month?. let say top ten for July 2016
You can get top whatever played only if history table contains data for the time period you need. The query will be relatively slow, because history table does not contain reference to track ID and you have to JOIN on artist and title columns to get song IDs.

This query will get top ten tracks for July:

-- Select Top10 tracks from July 2016
SELECT songs.ID, songs.artist, songs.title, COUNT(*) AS spins, SUM(history.listeners) as total_listeners
FROM `songs`
LEFT JOIN `history` ON (songs.title = history.title AND songs.artist = history.artist)

-- This is the date range filter
WHERE history.date_played BETWEEN '2016-07-01' AND '2016-08-01'

AND songs.song_type = 0

GROUP BY history.title, history.artist
ORDER BY spins DESC
LIMIT 10;


There is one issue with this query - it is not possible to include data for tracks that have been deleted from songs table, even if the data is present in history table. That's also caused by lack of track ID in history table.
I am, therefore I think.
I design and develop web apps, and have created few RadioDJ plugins - https://axellence.lv/downloads/

Filip83

This might be another one for the examples thread.
www.diskonektedmusic.com
www.soundcloud.com/diskonekted

Jhonny

Quote from: Filip83 on August 17, 2016, 04:49:23 PM
This might be another one for the examples thread.

:cool:

Em storing them to.
Never know when i will just put them in RadioDj.
R.T.F.M. means to me, Read The Fantastic Messages.
Einstein says: Make things simple, but don't make simple things simpler! (wise) this is just my life lesson you know.

I don't do drugs ( ͡° ͜ʖ ͡°)
The Radiodj tutorials site is now managed by Domstad radio .nl still  Just in English

Valdis

My HeidiSQL snippets folder contains about a dozen SQL scripts named starting with "RadioDJ". That's a good place to keep them for quick access.
I am, therefore I think.
I design and develop web apps, and have created few RadioDJ plugins - https://axellence.lv/downloads/

Marius

#11
Quote from: Valdis on August 17, 2016, 01:48:30 PM
There is one issue with this query - it is not possible to include data for tracks that have been deleted from songs table, even if the data is present in history table. That's also caused by lack of track ID in history table.

Since the history update procedure is not hard coded in the program , it not that complicated to create a new history column for trackID and modify the procedure to insert it there. If this is really needed.  :)

Create field for track ID:
ALTER TABLE `history` ADD COLUMN `trackID` INT NOT NULL AFTER `ID`;

Updated procedure:

DROP PROCEDURE IF EXISTS `UpdateTracks`;

DELIMITER $$

CREATE PROCEDURE UpdateTracks(IN trackID INT, IN tType INT, IN curListeners INT, IN historyDays INT, IN pWeight DOUBLE)
BEGIN

SET @tArtist = (SELECT artist FROM songs WHERE ID=trackID);

-- Update Count Played [MODIFIED]
UPDATE `songs` SET `count_played`=`count_played`+1, `date_played`=NOW() WHERE `ID`=trackID;

-- UPDATE ARTISTS
IF tType = 0 OR tType = 9 THEN
    UPDATE `songs` SET `artist_played`=NOW() WHERE `artist`=@tArtist;
END IF;

-- UPDATE REQUESTS
IF tType = 9 THEN
    UPDATE `requests` SET `played`=1 WHERE `songID`=trackID;
END IF;

-- DISABLE BY PLAYCOUNT
UPDATE `songs` SET `enabled`=0, `play_limit`=0 WHERE `enabled`=1 AND `play_limit`>0 AND `count_played`>=`play_limit` AND `limit_action`=1;

-- DELETE BY PLAYCOUNT
DELETE FROM `songs` WHERE `play_limit`>0 AND `count_played`>=`play_limit` AND `limit_action`=2;

-- UPDATE WEIGHT
IF pWeight>0 THEN
    UPDATE `songs` SET `weight`=`weight`-pWeight WHERE `ID`=trackID AND (`weight`-pWeight)>=0;
END IF;

-- UPDATE HISTORY
IF historyDays > 0 THEN
    INSERT INTO `history`(trackID, date_played, song_type, id_subcat, id_genre, duration, artist, original_artist, title, album, composer, `year`, track_no, disc_no, publisher, copyright, isrc, listeners)
    SELECT ID, NOW(), song_type, id_subcat, id_genre, duration, artist, original_artist, title, album, composer, `year`, track_no, disc_no, publisher, copyright, isrc, curListeners FROM `songs` WHERE ID=trackID;
END IF;

-- DELETE OLDER ENTRIES FROM HISTORY
DELETE FROM `history` WHERE TIMESTAMPDIFF(DAY, `date_played`, NOW()) >= historyDays;
END $$

DELIMITER ;
DOWNLOADS PAGE

HOW TO FIX RADIODJ DATABASE
----------------
Please don't PM me for support requests. Use the forums instead.

Valdis

Quote from: Marius on August 17, 2016, 05:20:45 PM
Since the history update procedure is not hard coded in the program , it not that complicated to create a new history column for trackID and modify the procedure to insert it there. If this is really needed.  :)
Yes, but most users are not that good with SQL and it is easy to mess up the procedure by one small typo. It would be better to add the column in default installed table structure.
I am, therefore I think.
I design and develop web apps, and have created few RadioDJ plugins - https://axellence.lv/downloads/

Marius

I agree, but for now this is the situation. The next version has the track ID column in history, but for the current version i can't do much.
DOWNLOADS PAGE

HOW TO FIX RADIODJ DATABASE
----------------
Please don't PM me for support requests. Use the forums instead.

Valdis

#14
Quote from: Marius on August 17, 2016, 05:20:45 PM
Updated procedure:

DROP PROCEDURE IF EXISTS `UpdateTracks`;

DELIMITER $$

CREATE PROCEDURE UpdateTracks(IN trackID INT, IN tType INT, IN curListeners INT, IN historyDays INT, IN pWeight DOUBLE)
BEGIN

SET @tArtist = (SELECT artist FROM songs WHERE ID=trackID);

-- Update Count Played
UPDATE `songs` SET `count_played`=`count_played`+1, `date_played`=NOW() WHERE `ID`=trackID;

-- UPDATE ARTISTS
IF tType = 0 OR tType = 9 THEN
    UPDATE `songs` SET `artist_played`=NOW() WHERE `artist`=@tArtist;
END IF;

-- UPDATE REQUESTS
IF tType = 9 THEN
    UPDATE `requests` SET `played`=1 WHERE `songID`=trackID;
END IF;

-- DISABLE BY PLAYCOUNT
UPDATE `songs` SET `enabled`=0, `play_limit`=0 WHERE `enabled`=1 AND `play_limit`>0 AND `count_played`>=`play_limit` AND `limit_action`=1;

-- DELETE BY PLAYCOUNT
DELETE FROM `songs` WHERE `play_limit`>0 AND `count_played`>=`play_limit` AND `limit_action`=2;

-- UPDATE WEIGHT
IF pWeight>0 THEN
    UPDATE `songs` SET `weight`=`weight`-pWeight WHERE `ID`=trackID AND (`weight`-pWeight)>=0;
END IF;

-- UPDATE HISTORY
IF historyDays > 0 THEN
    INSERT INTO `history`(date_played, trackID, song_type, id_subcat, id_genre, duration, artist, original_artist, title, album, composer, `year`, track_no, disc_no, publisher, copyright, isrc, listeners)
    SELECT NOW(), trackID, song_type, id_subcat, id_genre, duration, artist, original_artist, title, album, composer, `year`, track_no, disc_no, publisher, copyright, isrc, curListeners FROM `songs` WHERE ID=trackID;
END IF;

-- DELETE OLDER ENTRIES FROM HISTORY
DELETE FROM `history` WHERE TIMESTAMPDIFF(DAY, `date_played`, NOW()) >= historyDays;
END $$

DELIMITER ;


An there it is, the typo I'm talking about. I get following error:
SQL Error (1054): Unknown column 'trackID' in 'field list'

That should be `ID` instead of `trackID` in the last SELECT query.


Update: If anyone is going that route and adding trackID to history table and the procedure, they should run a query to synchronise the new trackID from songs table:

UPDATE `history`
INNER JOIN songs ON (history.title = songs.title AND  history.artist = songs.artist AND history.album = songs.album)
SET history.trackID = songs.ID
WHERE history.trackID = 0;

Not 100% perfect but at least the trackID values will point to existing tracks rather than be a useless zero value.
I am, therefore I think.
I design and develop web apps, and have created few RadioDJ plugins - https://axellence.lv/downloads/