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

Failure

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for failure. Thank you for the good days and the bad days. Thank you for the journey. There are no failures, only lessons learnt. Better to try and fail than fail to try. For this chapter I made a bunch of notes and pieced them together. If mosaic was a style of writing, this would be it for me. Weirdly enough I am not afraid of failure as much as I am afraid of success. I want to live an ordinary and simple life. Much of what society deems as success is really the rat race. I guess I do not have a problem with success but the mainstream definitions of success. What does failure teach us? Failure teaches us to never give up because success can be right around the corner. Define your own definition of success. Enjoy the journey. Focus on learning. Focus on gaining experience. Focus on becoming wise. Today I learnt how a pearl is formed. A pearl forms when an oyster responds...

Wisdom

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for wisdom. Age brings wisdom. At least it should. Everyday we are learning. Everyday we are growing. What is the wisest thing ever said? I think one of the wisest things ever said is something my grandfather told my mother which is, "Dont make nothing a problem." Let me digest what was meant by this. Life happens. Fix what we can. Let go of what we cannot fix. Fix what is within our control. Get help when needed. Let go and let God. There are no problems only solutions waiting to be found and lessons waiting to be learnt. Every problem has a solution. Do not look at the problem, look at the solution. Do not see the problem, see the solution. What defines the situation is not the problem but the solution. We get to choose how we react to what we face in life. They say that laughter is the best medicine. If we do not laugh we will cry. So sometimes wisdom comes wi...

Faith

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for faith. We say we want to meet God but we already do in some ways. We meet God in his creation. We meet God every time we meet someone. We meet God every time we meet the animals. We meet God every time we meet nature. God is all around. Faith is when we can see this. This is God's world. We are God's work. We are testimony of his greatness. Everyday that goes by I think about God. Everyday I thank God for his mercy. I take my problems to God. I pray to God. God is at the center of my life. Nothing happens without the will of God and therefore I would be wise to submit to the will of God. God is in the everyday moments. God loves when we love. What is the simplest definition of faith? I think it is found in the phrase, let go and let God. I asked my friend the same question. My friend says that at its simplest, faith is trust in what you cannot see or prove. But...