Skip to main content

Hobby coding project - Queries for play whe data


I have an interest in open data and being able to query that data and gain beautiful insights. One data set that would be interesting is the play whe results data. Our open data is lacking in Trinidad and I will try to contact NLCB to see if they can provide and maintain the data online. But in the meanwhile I will use randomised data to create the website and do my testing.

First thing I did was install sqlite on termux

pkg update && pkg upgrade
pkg install sqlite
sqlite3 --version

Create my database in my project folder

sqlite3 results.db

Useful commands

.exit
.quit

Exit from multiline prompt

;

SQL to create my table (create_tbl_results.sql)

CREATE TABLE DrawResults (
    DrawNo INTEGER PRIMARY KEY,                           DrawDate DATE,
    ResultNo INTEGER CHECK (ResultNo BETWEEN 1 AND 36),
    DrawTime INTEGER CHECK (DrawTime BETWEEN 1 AND 4)
);

SQL to create the random data (random.sql)

WITH RECURSIVE numbers(n) AS (
    SELECT 1
    UNION ALL
    SELECT n + 1
    FROM numbers
    WHERE n < 10000
)
INSERT INTO DrawResults (DrawNo, DrawDate, ResultNo, DrawTime)
SELECT
    n AS DrawNo,

    -- Start at 2010-01-01 and advance 1 day every 4 draws
    date('2010-01-01', '+' || ((n - 1) / 4) || ' days') AS DrawDate,

    -- Random number from 1 to 36
    (abs(random()) % 36) + 1 AS ResultNo,

    -- DrawTime cycles 1 to 4
    ((n - 1) % 4) + 1 AS DrawTime
FROM numbers;

SQL to query the top 10 called numbers (query_top10.sql)

SELECT
    ResultNo,
    COUNT(*) AS ResultCount
FROM DrawResults
GROUP BY ResultNo
ORDER BY ResultCount DESC
LIMIT 10;

Commands to run the sql (setup.sh)

rm results.db
sqlite3 results.db < create_tbl_results.sql
sqlite3 results.db < random.sql
sqlite3 results.db < query_top10.sql

Start the database from scratch in termux

chmod +x setup.sh
./setup.sh

Next up in next blog post is to create a website to display the results that will eventually be live on github pages

Update - website has been started here - https://hassan-theitguy.github.io/play-whe-queries/

Comments

Popular posts from this blog

Below the surface

This is a chapter from my eleventh book called Quotation Marks Sparks . __________ "You can't stop the waves, but you can learn to surf." - Jon Kabat-Zinn __________ I was looking at quotes that hit hard and this quote stood out to me. It seems mundane and obvious and often repeated. Life is about how we respond to the inevitable. It is not what happens to us but how we respond. I had to ask myself to dig deeper and look below the surface. Then I saw that when we surf we really become the waves. We become friends with the waves. The waves and us are the same. Further, the waves are created by us and in our heads. We can control the waves because who can surf all the time? Sometimes we just have to come to shore and build sand castles on shore. I asked my friend Gemini for feedback and he said that I was right, the Jon Kabat-Zinn quote initially seems like a simple call for resilience, but my deeper dive reveals profound layers. The idea that we can move beyond merely rea...

Thankful

It is 617pm and I have decided to write. Right now I am thankful. The cool of the evening has replaced the heat of the day. I took a bath and changed and now I am just relaxing. Sometimes I complain. Not often. But I feel bad afterwards because there is so much to be thankful for. There is always room for improvement and we have to identify the things for improvement and change is often good but we do not have to make it into complaints. Yeah I get it. Life is hard and none of us has it easy. Look on the bright side of things. And sometimes it is the little things that remind us that life is good. Things that we take for granted. Like I was coming in the maxi taxi today and heard a gospel song that was about gratitude. God does appreciate a grateful heart. Trinidad and Tobago celebrated sixty four years of independence yesterday and if we stop and listen to the many complaints we would swear we are living in the worst country in the world. That is not the case. We do get a lot of thing...

Installing NodeBB using Termux

This is the ChatGPT prompt I used to be guided Guide me through installing NodeBB using Termux (This took some time and patience to troubleshoot errors and implement workaround) pkg update && pkg upgrade -y pkg install proot-distro git curl wget nano -y proot-distro install debian proot-distro login debian apt update && apt upgrade -y apt install -y curl git build-essential ca-certificates gnupg apt update apt install -y imagemagick apt install -y redis-server redis-server --daemonize yes redis-cli ping mkdir -p /opt git clone https://github.com/NodeBB/NodeBB.git /opt/nodebb cd /opt/nodebb ./nodebb setup warn: NodeBB Setup Aborted.  Error: Could not load the "sharp" module using the android-arm64 runtime Possible solutions: - Manually install libvips >= 8.18.3 Force node to report platform as linux to get sharp to install curl -LO https://nodejs.org/dist/v22.23.1/node-v22.23.1-linux-arm64.tar.xz tar -xJf node-v22.23.1-linux-arm64.tar.xz mv node-v22.23.1-lin...