2020 Advent of Code–Day 1

This series looks at the Advent of Code challenges.

I started the Advent of Code at the beginning of December 2020, but life quickly got in the way. Weekends especially, where I try to get away from the computer, so I fell behind. However, I did work through a few, and one of my goals in 2021 is to get through all of them.

I’m going to document my solutions on my blog.

Day 1

The first thing I did was set up a template for the solutions. This is clearly important, and I used some basic ASCII art.

2021-01-22 12_23_37-Day1.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (53)) - Microsoft SQL Server Manag

From here, I tackled the challenge. This is one suited for databases, as there is the need to take a list of numbers and find two that add up to 2020. I created a simple table that contained a single column to store numbers.

CREATE TABLE Day1
(  datavalue INT)
GO

In here I inserted the test data from the challenge.

The easy way for me to tackle this quickly was cross join the numbers with a sum. I put this in a CTE, which gives me the sum of all individual numbers.

WITH cteCalc (a, b, sumab)
AS (   SELECT
                       a.datavalue, b.datavalue, a.datavalue + b.datavalue AS sumoftwo
        FROM
                       Day1 a
            CROSS JOIN day1 b)

Once I had this, in the outer query I added a WHERE that limited the results to the sum being equal to 2020, and for the column list, I produced the product.

WITH cteCalc (a, b, sumab)
AS (   SELECT
                       a.datavalue, b.datavalue, a.datavalue + b.datavalue AS sumoftwo
        FROM
                       Day1 a
            CROSS JOIN day1 b)
SELECT a, b, a * b AS solution
FROM cteCalc WHERE sumab = 2020;
go

This gave me the result.

As a hint, I used BULK INSERT to load the complete data from the test file into my table.

Part 2

Each challenge has two parts, with the same data. In this one, I had to find 3 entries that summed to 2020. I just added another cross join and this was solved.

WITH cteCalc (a, b, c, sumabc)
AS (   SELECT
                       a.datavalue, b.datavalue, c.datavalue,
                       a.datavalue + b.datavalue + c.datavalue AS sumoftthree
        FROM
                       Day1 a
            CROSS JOIN day1 b
            CROSS JOIN day1 c
    )
SELECT a, b, c, a * b * c AS solution
FROM cteCalc WHERE sumabc = 2020;
GO

All in all, an easy day. Now I need to solve this in Python.

Posted in Blog | Tagged , , , | 6 Comments

Remembering the Past

Just a short time ago, the SQL Family lost Gareth Swanepoel. It was fairly sudden and sad to have someone I knew fairly well pass away. He was the first person that I directly knew that was lost to the COVID-19 pandemic.

I’m getting older. I seem to hear about a person I’ve known passing away almost every year now. I know I’ve got more life behind me than in front of me, and I cherish the moments I do have and the memories of others.

A few years ago Andy Warren and I were talking about this subject. This was after we lost a common friend, and we wanted to have some way to remember them. We kicked around a few ideas, but other than adding an “Obituaries” category to Database Weekly, I didn’t do much.

I decided to change that after I heard about Gareth. While processing my grief, I bought sqlmemorial.org and started to put something together. I’ve been hearing about Jekyll and I spent a little time messing around with it. I ended up deciding to put this up and start ensuring that we have a way to remember the people that have taught and touched us. I’ll also share some articles on how I put this together.

I set this up as a GitHub repo, and I am taking pull requests for people that want to add a memory, a link, a picture, or something else. I’m also hoping that people will let me know as others pass, a sad fact of life that we will continue to experience in the years to come.

I’m hoping this helps me, and many of you, fondly remember the people in our SQL Family.

Steve Jones

Posted in Editorial | Tagged , , | Comments Off on Remembering the Past

Daily Coping 27 Jan 2021

I started to add a daily coping tip to the SQLServerCentral newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag. 

Today’s tip is to get back in contact with an old friend you miss.

I’ve had a great life and been able to meet many people. Some of these have become friends, and at times, life has taken us in different directions. Some of my friends are people I used to work with, and for a few of these, I’ve made time to visit them when I am in their area.

That hasn’t happened in a long time, well over a year at this point.

I reached out to someone recently that I haven’t seen in a long time, just to touch base, say hi, and have a conversation. It was 10 minutes out of my day, but 10 minutes that made me smile. I hope they can say the same thing.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 27 Jan 2021

Live versus Stored Data

I’ve been working with various technologies as experiments over the last few years. I’m curious how useful I find them, as well as how they might help the clients and customers I run into at Redgate Software.

I think Jupyter notebooks are a technology that has a lot of promise, and their use is growing in many organizations. One of the interesting things with notebooks is they can store results inside them, which allows a transfer of information in an interesting fashion. If I re-run a cell, the results can change and comparing them is challenging, but that is a separate issue. At least I can capture the results and share them.

I’ve also been working with Power BI at times. I saw a demo of a query from PBI to Excel, where the data was then stored in the PBIX file. This query was disconnected, but it could be refreshed. You can also configure how this works, so you get the choice of live or stored data.

If you are distributing information to users, those of us in the database world often think about having live data available and queried from a database, but often the same data gets queried over and over, which creates a load on our system. I know the data is often cached in these situations, but cache is a previous resource, so limiting the repeat queries can be valuable.

I do think this is a decision point for some applications, where we might choose to limit the amount of live data v stored data. There are times when speed matters more than having exact data, so cached or stored data works well. Sometimes the most current data is critical, and you need to query the database.

How do you decide when you want a live connection to a database, and when stored data is acceptable. I don’t know that I have any rules, but I evaluate each situation and try to work with users to make a decision. I suspect most people do the same thing, but if you always use live data, let me know today.

Steve Jones

Listen to the podcast at Libsyn, Stitcher, Spotify, or iTunes.

Posted in Editorial | Tagged | Comments Off on Live versus Stored Data