Author: way0utwest

  • 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.

  • Daily Coping 28 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 notice when you feel judgmental and be kind instead.

    The pandemic has been hard on all of us, in various ways. However, the one thing I’ve learned is that my view isn’t like other people’s views. We don’t live in the world the same way, and we don’t experience things the same.

    I was out and about not too long ago, and in the place I was visiting, they required masks to be worn. That’s a bit of a change from most of my life, as a lot of Colorado allows you to forego a mask if you’re vaccinated. I had to walk back to my car, find a mask, and then go inside.

    Easy to be take things the wrong way, but I just went along with the flow. I hate masks, but it’s not that hard to wear one.

    In this case, while talking with one of the employees, or maybe owner, he noted that he’d gone through cancer treatment not too long ago and was still worried about his recovery. A valid reason, and perhaps a good one to ask others to wear masks. Not even because of COVID, but because any infection could be problematic.

    I could have judged this person and dismissed their concerns, but it was easier to wish them well and comply with the request to wear a mask.

  • Daily Coping 27 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 see something good about something that went wrong.

    I was coming home from vacation recently and our flight got delayed and delayed. We went from a 2 hour, to a 3 hour, to a 5 hour delay. Then the flight was moved to the next morning, early at 6am, which meant we needed to find lodging and get up incredibly early.

    After quite a few hours in the airport, we grabbed a taxi and went to a hotel. No reasonably priced hotels nearby, so a much longer ride than we wanted, no restaurant at the hotel or anything close, and by the time we arrived, we were only about 8 hours from needing to leave again.

    Easy to complain and be upset. It was better to appreciate that I got another night with my wife away from life, and we could afford to deal with the issues, and we’d gotten some food at the airport earlier and had some snacks in bags. We’d seen friends in the airport and had time to chat with them, which hadn’t happened much this past year.

    Looking at the positives reduced stress and reminded me that life was good that day, even with a few bumps in the road.

  • The Post Pandemic Office Experience

    I traveled to Pasadena, CA this week to work with a few other Redgaters in an office. There was a get together planned this week for the entire staff, many of whom have not seen each other in a year. Some have never met. When I heard about this, I requested permission to go. My request was approved, and I spent a couple of days in our LA WeWork space.

    I had mentioned I was coming in our Slack channel, and reached out to a few people, hoping to get some people in the office while I was there. I flew out early one morning, with a view I hadn’t seen in a long time.

    20210721_094016

    I enjoy flying into LA, crossing the mountains and desert, seeing landmarks along the way, watching the transition from nature to very developed (and large) city. It always makes me smile. In this case, I arrived and quickly got a Lyft up to the office. Along the way I had gotten a note from someone that masks were needed in the common spaces, and a few phone numbers to text for admission.

    I wasn’t sure what to expect, as I’ve seen various incarnations of co-working spaces. In this case, WeWork is a large space that’s been cut down and separated with lots of glass walls. There is a lot of security in our building, and I needed someone to use a card to activate the elevator, as well as leave the elevator lobby area. Inside, there are lots of small offices, containing 1, 2, 4, or more desks. While it’s a space where you can see far through the glass walls, it’s also small as there are lots of walls.

    Here’s an example of a larger space, with 5 desks, but I saw plenty of much smaller offices with 1 or 2 desks.

    20210721_150957

    Redgate has their own space, which is separated from others. Almost all offices have a separate key card entry system.

    20210721_152106

    My first day, there were 4 of us in the office. I knew them all, and I contact them regularly on Slack or in Zoom calls, but it was nice to sit and be able to chat with them in person. Inside our offices, we didn’t have to wear masks, but the coffee machine and bathroom were outside, so I kept mine handy in a pocket.

    We did have a small snack area, which was nice. It felt a little abandoned though, with few of us around.

    20210721_152121

    There were really 4 separate areas of desks, and I had one to myself. I almost started to set up next to someone, but then a few people got on the phone and I realized how loud it can be. All the glass and relatively low ceilings make it hard to work in a group, at least in our space.

    20210721_111949

    We went out to lunch, and took some breaks to talk about work or life, and it felt normal, albeit rather limited with so few people.

    The second day was better, with about 10-12 people in the office. It was much louder and a little harder to work, but it felt like a busy organization again. I liked it, and was glad I had the chance to experience it.

    As with many things during this pandemic, things changed. There was an order from the Pasadena Health Department that took effect between my first and second days noting that we had to wear masks, even in our own space. Annoying, but not that much of a problem. I suspect that might keep some people from coming back, or at least make them think about coming less frequently.

    We also had a get-together at a local pub. We were outside, and most of the California staff came. It was interesting in that 8-10 were people no one had met. People hired during the pandemic, who had been working 100% remote while with the company. That was a little strange, but it’s how life goes.

    We talked, laughed, and appreciated the time to get together. I realize that I do enjoy going to an office some time, and I’m hoping I’ll get back to Pasadena 2-3 times a year. It was a good trip, long and tiring for me with two early flights, but great for my mental health, and a way to catch up with people that I haven’t seen in a long time.

    I hope more people come back, at least part time, as it seemed we all missed the chance to get together and talk.