Category: Blog

  • Daily Coping 22 Apr 2022

    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 try a new online exercise, activity, or dance class.

    I like going to the gym for Yoga 2-3 times a week. However, sometimes my schedule is a bit busy and I can’t find the time to go. I’ve written about using Yoga with Tim or Yoga with Adriene on YouTube to fill in those busy days.

    I also have a Lifetime membership that includes some livestreamed and on-demand classes. I’ve never used those, but I decided to give one a try for this tip and see how it might help me cope with a busy schedule.

    My wife and I had done a kickboxing class a few weeks back, and there is a similar on-demand class called Strike. I have some travel this week and my plan is this morning, Friday, to try this class early in the morning before I have to go to work. Rather than sit on a bike, this will my coping activity today.

    Update: I did the Strike class and it was an OK workout this morning. 30 minutes and an average heart rate of 122bpm for the session.

  • Daily Coping 21 Apr 2022

    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 go exploring around your local area and notice a few things.

    I tend to drive the same routes all the time as live is busy. I am often focused on just getting from point A to point B rather than the journey on a regular basis.

    I was in Chicago last weekend and we had the chance to ride the train from our hotel to the convention center. Part of the route is above ground, leaving the Loop, and I took time with my wife to look at the wonderful architecture and beautiful buildings of Chicago. It really is a neat city, visually. We pointed out interesting features as we went by, and loved the Roosevelt University building, where my daughter was recruited and considered attending.

    One day we took a taxi and this drive too a route that cut through some neighborhoods, and I noticed how any balconies exist on buildings. Even high rises. For a city with cloudy, windy, and rainy weather, they really do take every opportunity to get outside.

  • A Monthly Running Total–#SQLNewBlogger

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

    Recently I was looking at some data and wanted to analyze it by month. I have a goal that is set for each day and then an actual value. I wanted to know how I was tracking against the goal, as a running total. If my goal is 10 a day, then I ought to actually get to 10 the first day, 20 for the second day (10 + 10), etc.

    Here is some data that I am using, showing the date, the actual, and the estimate:

    2022-04-18 08_54_02-SQLQuery1.sql - ARISTOTLE_SQL2017.way0utwest (ARISTOTLE_Steve (53))_ - Microsoft

    The estimate is constant, so a running total is just the sum of all previous rows. The actual is similar, though in both cases, I want to reset this for each month. If I did a straight sum of all previous rows, I’d see something like this:

    2022-04-18 08_56_31-SQLQuery1.sql - ARISTOTLE_SQL2017.way0utwest (ARISTOTLE_Steve (53))_ - Microsoft

    I don’t want this. Instead, I want something that’s like this:

    2022-04-18 08_57_25-SQLQuery1.sql - ARISTOTLE_SQL2017.way0utwest (ARISTOTLE_Steve (53))_ - Microsoft

    This is fairly easy to do with window functions in T-SQL. I use a SUM() for each column with an OVER() clause. In this case, I partition by the year and month, which means that when those items change, we reset a new set of values. Here is the query that produces the correct data above:

    SELECT
       spt.ProductionDate
    , SUM (spt.Actual) OVER (PARTITION BY
                                YEAR (spt.ProductionDate)
                              , MONTH (spt.ProductionDate)
                              ORDER BY spt.ProductionDate
                     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS AcutalRunningTotal
    , SUM (spt.Estimate) OVER (PARTITION BY
                                  YEAR (spt.ProductionDate)
                                , MONTH (spt.ProductionDate)
                                ORDER BY spt.ProductionDate
                     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS EstimateRunningTotal
    FROM dbo.SolarPowerTracker AS spt;

    This creates a window for each month (based on year and month) and groups all the data with the same values together. Then I get the sum as a running total. I also want a rows clause to be sure this works as intended.

    Update: Someone noted this might not be clear how this works, so I’ll do another post on more details of the query itself. FWIW, another good way to get moving with SQLNewBlogger and add new posts to add detail.

    I’ve added the CREATE and INSERT statements here:

    CREATE TABLE [dbo].[SolarPowerTracker]
    ( ProductionDate DATE CONSTRAINT SolarPowerTrackerPK PRIMARY KEY
    , Actual NUMERIC(10, 2)
    , Estimate NUMERIC(10, 2));
    GO
    
    INSERT INTO dbo.SolarPowerTracker
    (ProductionDate, Actual, Estimate)
    VALUES
    ( N'2022-02-23', 11.7530, 41.65 ), 
    ( N'2022-02-24', 46.7710, 41.65 ), 
    ( N'2022-02-25', 71.2480, 41.65 ), 
    ( N'2022-02-26', 72.0820, 41.65 ), 
    ( N'2022-02-27', 69.8990, 41.65 ), 
    ( N'2022-02-28', 69.0050, 41.65 ), 
    ( N'2022-03-01', 68.9900, 43.96 ), 
    ( N'2022-03-02', 65.1330, 43.96 ), 
    ( N'2022-03-03', 61.1790, 43.96 ), 
    ( N'2022-03-04', 33.2930, 43.96 ), 
    ( N'2022-03-05', 10.1330, 43.96 ), 
    ( N'2022-03-06', 0.6170, 43.96 ), 
    ( N'2022-03-07', 4.2670, 43.96 ), 
    ( N'2022-03-08', 47.7440, 43.96 ), 
    ( N'2022-03-09', 11.5580, 43.96 ), 
    ( N'2022-03-10', 0.6470, 43.96 ), 
    ( N'2022-03-11', 15.4400, 43.96 ), 
    ( N'2022-03-12', 70.3260, 43.96 ), 
    ( N'2022-03-13', 61.3710, 43.96 ), 
    ( N'2022-03-14', 74.5110, 43.96 )

     

    SQL New Blogger

    As I was working on this query, I realized it wasn’t complex, but it was something unusual. Often I’ve done totals for a time period that a user supplies, not a set one like a month with a reset each month. I thought this was a good way to showcase how to solve this relatively simple problem.

    I spent about 15 minutes taking my code and then writing this post to show how I solved a a problem. This is something you could add on your blog to showcase your knowledge on solving a specific problem, not a general one.

  • Daily Coping 20 Apr 2022

    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 active singing today (even if you can’t sing).

    I can’t sing. My wife says I’m not too bad if I’m not trying to hit the notes, but I don’t think that’s an endorsement.

    Fortunately, I am often in the car alone running errands. I have a few playlists, but here are the ones I’ll sing out loud today: