Skip to content

Instantly share code, notes, and snippets.

@tfgrahame
tfgrahame / stats.ttl
Last active April 27, 2020 09:19
Example stats model
@prefix sport: <http://www.bbc.co.uk/ontologies/sport/> .
@prefix stat: <http://example-ontology/statistics/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<event1> a sport:SportsCompetition ;
sport:awayCompetitor <team1> ;
sport:homeCompetitor <team2> ;
stat:hasTeamStatistics <teamStats1> , <teamStats2> .
<team1> a sport:CompetitiveSportingOrganisation ;
SELECT
s.service_id, COUNT(s.service_id)
FROM
service s
LEFT JOIN
(SELECT
s.service_id
FROM
service s
JOIN broadcast b ON s.service_id = b.service_id
@tfgrahame
tfgrahame / news-hierarchy.sql
Created June 22, 2018 10:38
news-hierarchy.sql
SELECT
pp.parent_pid,
COUNT(pp.child_pid) AS 'episode_count',
versions.version_count,
broadcasts.broadcast_count
FROM
pip_pip pp
JOIN
(SELECT
pp.parent_pid AS 'brand_pid',
@tfgrahame
tfgrahame / duplicate-uids.sql
Created June 22, 2018 09:52
duplicate-uids.sql
SELECT
id.value, COUNT(id.entity_pid)
FROM
identifier id
WHERE
id.type = 'uid'
GROUP BY id.value
HAVING COUNT(id.entity_pid) > 1
ORDER BY id.created DESC
@tfgrahame
tfgrahame / group_concat-genres.sql
Created May 14, 2018 10:07
group_concat-genres
SELECT DISTINCT
pg.pid,
GROUP_CONCAT(g.topleveltext,
' | ',
g.secondleveltext,
g.thirdleveltext
SEPARATOR ' | ') AS 'genres'
FROM
pip_genre pg
JOIN
@tfgrahame
tfgrahame / replace_parent_brand.py
Last active April 19, 2018 09:04
Replaces the parent brand of a clip
#!/usr/bin/env python
from requests import get, put
import os
import argparse
import xml.etree.ElementTree as ET
parser = argparse.ArgumentParser(description='Entities and their pids.')
parser.add_argument('child_pid', type=str, help='The pid of the child entity')
parser.add_argument('parent_pid', type=str, help='The pid of the parent entity')
@tfgrahame
tfgrahame / audio-clips.sql
Created January 3, 2018 10:07
audio-clips.sql
SELECT
YEAR(cl.created),
CASE
WHEN MONTH(v.created) = '1' THEN 'Jan'
WHEN MONTH(v.created) = '2' THEN 'Feb'
WHEN MONTH(v.created) = '3' THEN 'Mar'
WHEN MONTH(v.created) = '4' THEN 'Apr'
WHEN MONTH(v.created) = '5' THEN 'May'
WHEN MONTH(v.created) = '6' THEN 'Jun'
WHEN MONTH(v.created) = '7' THEN 'Jul'
@tfgrahame
tfgrahame / podcasts.sql
Created January 3, 2018 10:06
podcasts.sql
SELECT
YEAR(v.created) AS 'year',
CASE
WHEN MONTH(v.created) = '1' THEN 'Jan'
WHEN MONTH(v.created) = '2' THEN 'Feb'
WHEN MONTH(v.created) = '3' THEN 'Mar'
WHEN MONTH(v.created) = '4' THEN 'Apr'
WHEN MONTH(v.created) = '5' THEN 'May'
WHEN MONTH(v.created) = '6' THEN 'Jun'
WHEN MONTH(v.created) = '7' THEN 'Jul'
@tfgrahame
tfgrahame / available-hours-month.sql
Created January 3, 2018 10:04
available-hours-month.sql
SELECT
fbs.service_id,
fbs.year,
CASE
WHEN MONTH(fbs.published_date) = '1' THEN 'Jan'
WHEN MONTH(fbs.published_date) = '2' THEN 'Feb'
WHEN MONTH(fbs.published_date) = '3' THEN 'Mar'
WHEN MONTH(fbs.published_date) = '4' THEN 'Apr'
WHEN MONTH(fbs.published_date) = '5' THEN 'May'
WHEN MONTH(fbs.published_date) = '6' THEN 'Jun'
@tfgrahame
tfgrahame / aggregate-original-hours-per-month.sql
Created December 20, 2017 11:48
aggregate-original-hours-per-month.sql
SELECT
fbs.service_id,
fbs.year,
CASE
WHEN MONTH(fbs.published_date) = '1' THEN 'Jan'
WHEN MONTH(fbs.published_date) = '2' THEN 'Feb'
WHEN MONTH(fbs.published_date) = '3' THEN 'Mar'
WHEN MONTH(fbs.published_date) = '4' THEN 'Apr'
WHEN MONTH(fbs.published_date) = '5' THEN 'May'
WHEN MONTH(fbs.published_date) = '6' THEN 'Jun'