Author: way0utwest

  • A Speedy Digital Overreaction

    I lost my wallet a few weeks ago. I realized that I didn’t have it on a trip to town and was able to use my phone to pay for something. I assumed I’d left it at home in another pair of pants or on my desk.

    We were leaving for a trip the next morning, and I was a bit stressed about it being misplaced. I kept looking around the house, thinking I set it somewhere, checking clothes, bags, etc. I even called a the volleyball gym and asked them to look for it. I checked my accounts, and the cards hadn’t been used, so I turned them off and kept thinking I’d misplaced it.

    Eventually I had to give up and just go, without any forms of payment for the road. Fortunately my wife had her card, as my daughter forgot to bring hers. We had a fun weekend of sharing a single card as we tried to navigate a world that is mostly card based in the US. We forgot the card one afternoon and couldn’t go to a museum, which only accepted cards.

    On the way down, while going through many emotions, I finally accepted that a few hundred dollars were gone and my cards needed replacing. I was traveling in 6 days, so I needed to start preparing. I was able to replace my bank cards and my license online, getting replacements shipped out the next day. It was amazing how quickly and easily I could get this accomplished using my mobile.

    An Interesting Recovery

    Even though I’d accepted the loss, I kept thinking about my wallet. My best guess was that I’d set it down at home and then it fell behind or near a place I didn’t check. I also kept thinking it was strange that no one had tried to use a card if I’d left it in a public place.

    Eventually one of my kids suggested I call Wal-Mart. That was the last place I’d used it, and perhaps they had it. I called them, and surprisingly, a manager called back to say it was there. I drove down, trying not to get my hopes up.

    They did have it, and I recovered it, all cards and cash intact. A relief, and somewhat amazing. Fortunately all my cards arrived quickly, and I was able to travel with all the cards I expected to have, albeit with new numbers that I hadn’t memorized.

    Lessons Learned

    The big lesson is that I should pay more attention to my wallet. I don’t know if I left it on the self-checkout (most likely) or in the cart (less likely), but I can’t let my attention wander.

    The second one is that there are good people out there, and I should remember that.

    Third, I need a wallet inventory. There were a few things, like insurance cards, that I didn’t consider with regards to replacement. That might prove problematic, so I’m grabbing digital images of these cards to keep around, just for my knowledge.

    Fourth, the use of digital NFC payments is by no means universal. Even getting cash out of an ATM isn’t smooth here. Especially once I’ve cancelled the cards. I should have left one enabled, but turned off. I could always turn it on for a few minutes to get cash..

    Fifth, I should keep some sort of phone/email identifier in my wallet. If someone were to find it, at least they could attempt to contact me.

    Sixth, I’m going to try a tracker of some sort. This isn’t the first time I’ve misplaced my wallet, though it’s usually somewhere in the house or in a family member’s car. I don’t know how well these work, but I’m not in the completely digital world, so we’ll see if this helps me to stop losing things.

  • Daily Coping 30 Jul 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 think about what you learned from a recent problem.

    I had a problem recently. Actually, it’s an often repeating problem in my life. I schedule too many things and get too busy, making me feel stressed, rushed, and behind on things that are important to maintaining life.

    In this case, I’d taken too many evenings off from making small steps in chores. I’d get done with work, or the gym, and start relaxing a bit earlier than I should have. Not that I needed to do a lot of work, but 30 minutes would have made a big difference across a few nights.

    I’m learning to not save a chore until I have time to get it all done. I can tackled it in small stages and even if it’s partially finished, like my front bushes were a few weeks ago. In this case, I should have tackled part of my lawn across a few nights, and I’d have gotten at least half of it done.

    Maybe I’ll be better next time.

  • Daily Coping 29 Jul 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 write down a few things you are grateful even when life is hard.

    First, my wife. She supports me and helps me through tough times.

    Second, my health. Even when something is injured or aching, I’m fairly healthy. I can’t forget that.

    Third, I’ve found a hobby that keeps me moving, active, and engaged with kids.

  • Using Framing for a Running Total–#SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    I’ve been writing about window functions, which I find very handy. In this post, I want to build a running total using the framing of the window. This continues from my last post on aggregates.

    I’ve been looking at baseball data. Imagine that someone wants to know how many total home runs a player had at each stage of his career. In this case, if a player hit 1, then 4, then 5 home runs, we’d expect a running total to show:

    • Year 1: 1
    • Year 2: 5
    • Year 3: 10

    This is a cumbersome query without window functions, and inefficient, as I really need a subtotal query for each of the main rows. It’s difficult to write, read, and it’s slow. With a window function, however, I can use this query. You can see the framing with the ROWS section in the OVER() clause.

    SELECT
              yearid
            , hr
            , SUM (b.hr) OVER (ORDER BY b.yearID
                               ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS TotalHR
    FROM     dbo.batting AS b
    WHERE    b.playerID = 'griffke02'
    ORDER BY b.yearID;

    This gives me results that look like this:

    2021-07-23 15_53_08-window_queries.sql - ARISTOTLE.BaseballData (ARISTOTLE_Steve (57))_ - Microsoft

    Simple and easy to see and read.

    This works as the window is scanned and totaled using the OVER() clause. In this case, the partition is the entire window, meaning all rows for this player. I’ve set the ordering to be the year, so as we move through the years, the SUM() is calculated using the part of the window that goes from the beginning, with the UNBOUNDED PRECEDING marker, until the current row.

    You can think of this as we scan from year 1989 first. In this case, the entire preceding section is nothing, and the current row is 1989. Therefore the sum is 16.

    Next we look at year 1990. There is a preceding row (1989) and this current row. We sum those to get 16+22=38. We repeat this with each row, always going back to the first row.

    For the ROWS clause, we can use the between to determine the start and end portion of the partition that we scan. This means we can use:

    • unbounded preceding
    • unbounded following
    • current row
    • an integer

    We can combine these 4 choices to get what we need. If we were looking only for a best 3 year time frame of home runs, we could get a sum like this:

    SELECT
              yearid
            , hr
            , SUM (b.hr) OVER (ORDER BY b.yearID
                               ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS TotalHR
    FROM     dbo.batting AS b
    WHERE    b.playerID = 'griffke02'
    ORDER BY b.yearID;

    This gives me a grouping of the SUM for the current 1, as well as 1 before and after, for each row.  My results are shown below. For the first year, there is no preceding row, so we sum the current and next row, 16+22 for 38. For the second row, we have 16 preceding, 22 current, and 22 next, which sum to 60. You can check the math for others.

    2021-07-23 16_01_12-window_queries.sql - ARISTOTLE.BaseballData (ARISTOTLE_Steve (57))_ - Microsoft

    There’s more to this, but for now, a quick running total is using SUM() and then the ROWS BETWEEN UNBOUNDED PRECEDING and CURRENT ROW clause.

    SQLNewBlogger

    This was a quick 15 minutes to write. I took part of what I’d done in the last post, changed the query quickly, and then started to explain part of a clause. I’ll keep this around and use it to expand on some other places where the framing can be useful and affect how I work with data.

    A good chance for you to also show how you might build a running total, or even running count, with your own data.