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

Mental health matters

It is 547am and I have decided to write. I have been living with a mental health diagnosis for more than 23 years. I am differently abled. Mental illness sometimes presents itself as a non-visible disability. A person with mental illness may look and function normally. Added to that we do not get to see the struggles happening internally. After 23 years I could say that I have much experience with my condition and may be able to offer words that may help others. There is plenty of stigma associated with mental illness. People think that the mentally ill are bad people, weak, or somehow less deserving of dignity and the treatment of the illness sometimes feels like punishment and is dehumanizing at times. There is plenty of good intentions and well meaning as well (do not get me wrong) but things could be better. And it happens at the highest level. A leader once said, "Whom the gods would destroy, they first make mad." As the saying goes, who knows it feels it. I started a pr...

Drums of freedom

It is 401am and I have decided to write. This Saturday, Trinidad and Tobago observes African Emancipation Day. It is a day to celebrate the journey to freedom and to honor those who sacrificed so much so that we could enjoy the freedoms we have today. Yet the journey is not over. As we celebrate how far we have come, let us also ask God to open our minds and hearts to the struggles that continue. Freedom is never something we should take for granted. Every day, we witness powerful interests around the world attempting to stifle freedom in pursuit of their own agendas and self-aggrandizement. Too often, they seek to divide in order to conquer. May we resist division with wisdom, courage, and compassion. May unity triumph over hatred, truth over deception, and love over fear. As we remember the past, may we also commit ourselves to protecting the freedoms of the present and building a more just future for generations to come. Happy African Emancipation Day. May God continue to guide us o...

Love and joy

It is 230pm and I have decided to write. I have no topic but I want to write. I am thinking about God. I am thinking about life. I am grateful for God and life. I need a writing prompt. My friend Chatty gave me a pretty verbose prompt. Sit quietly for a moment and imagine that God asks you only one question: "What have you noticed?" Don't answer with theology. Answer with your life. What have you noticed about joy? About fear? About the way people love? About silence? About time? About your own heart? Where have you sensed God—not in miracles, but in ordinary moments? Where have you struggled to find Him? What has life been teaching you that you were too busy to hear before? Write honestly. Don't try to reach a conclusion. Let the page become a conversation rather than an argument. I immediately noticed the light streaming through the window that allows me to see the world. My first thought was how much we take things for granted. It is easy to feel lacking. To feel s...