Author: way0utwest

  • T-SQL Tuesday #168 – Mature Window Functions

    tsqltuesdayIt’s time for T-SQL Tuesday and I’m hosting this month. I usually do one a year, just because I can and being responsible for a month keeps me engaged in the party.

    This month my invitation is on Window functions and is described below.

    If you’d like to host T-SQL Tuesday, let me know. I have lots of openings in 2024 and I’m looking for someone with a blog, some creativity, and an idea for a technical topic that you’d like to see other people write about.

    Mature Window Functions

    We’ve had window functions in SQL Server for a decade now, since SQL Server 2012.

    This month I’m asking you to write on how window functions have made your life easier. A few ideas for you:

    • What problems have you solved with a window function? Bonus points for lead/lag/first_value/last_value
    • Have you used the SQL Server 2022 enhancements in any queries?
    • How has performance improved for you with a window function
    • Draw a picture of a window with a spatial function – more extra points

    Give us some specifics, with real world problems. Obfuscate the data, at least if you have my name in your dev system, but help others understand how they might solve a complex aggregate using a Window function. The more specific examples, the more others might get help from one of the posts.

    The Rules

    Not many rules, but a few of them.

    • Post between 00:00:00 and 23:59:59 on 2023-11-14
    • Include the logo above in your post
    • Link that logo to this post
    • Leave me a trackback or comment on this post (double check if you have this automated)
    • Post you URL on Twitter, LinkedIn, etc. with the hashtag #tsql2sday
    • Have fun
  • Creating a Self Referencing FK in a CREATE Statement–#SQLNewBlogger

    I had written about a FK in a CREATE TABLE statement recently, but the second half of this was that after the original question, the person asked if this would also work for a self-referencing FK. It does, and I wrote this to show that.

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

    Creating the FK

    The last post showed how to create the FK, but this works within a table as well. Let’s say I want to have an Employee table that links back one employee to another, who is their manager. That type of structure looks like this:

    CREATE TABLE [dbo].[Employee](
         [EmpID] [INT] NOT NULL,
         [EmpName] [VARCHAR](20) NULL,
         [MgrID] [INT] NULL,
      CONSTRAINT [EmployeePK] PRIMARY KEY CLUSTERED 
    (
         [EmpID] ASC
    )
    ) ON [PRIMARY]
    GO

    I can add a link that makes MgrID a FK reference by altering the code like this:

    CREATE TABLE [dbo].[Employee](
         [EmpID] [INT] NOT NULL,
         [EmpName] [VARCHAR](20) NULL,
         [MgrID] [INT] NULL,
      CONSTRAINT [EmployeePK] PRIMARY KEY CLUSTERED 
    (
         [EmpID] ASC
    ),
    CONSTRAINT FK_MgrID_EmpID FOREIGN KEY (MgrID) REFERENCES dbo.Employee (EmpID)
    ) 
    GO

    Easy.

    SQL New Blogger

    This is a post that took me less than 10 minutes to write. I changed the code from the previous post and wrote this right after the other one. The search and replace was the longest code part, and then the writing was quick, 5 minutes.

    This is a core skill for a DBA or developer. Write your own post to show how and why to build a self referencing FK for some scenario that you work with in your job, or in a project.

  • Don’t (Always) Be a Hero

    I saw a comic from Kendra Little recently, which reminded me of my first SQL Server crisis. I got hired as a contractor to support a large Novell network. In my first chance to work with SQL Server, a new database server running SQL Server 4.2 on OS/2 1.2 was installed on our network to support a new data entry application. This was mandated by a government legal change on Jan 1.

    As the junior person, I was supporting the installation by corporate developers on Dec 31 at 5 p.m. We finished getting things set up, ate dinner, and then I watched the developers do some smoke testing and prepping the application and database servers for go-live at midnight. It was exciting to me as a young professional to be part of this deployment.

    At around 12:30 a.m., as I was getting ready to leave, the system locked up. We found the database server unresponsive, so we rebooted it. This continued to happen, with my eventually paging my boss and having him come in the early morning. We worked through the night and into the next day. I didn’t leave until early morning on Jan 2nd, with the need to return that night. We had an overloaded, unstable system that required us to work long hours babysitting the server. It was worse for our clients, who had to manually record data and then try to perform data entry during slow times to catch up the system. I was a hero that January, as were my co-workers, with all of us logging around 100 hours each week that month.

    It was a rough time, though I learned a lot about SQL Server, OS/2, and eventually, Windows NT. I also learned about poor software testing, especially load evaluation. I learned how easy it was to overwork yourself, and how heroics are needed but can’t be business as usual. Being a regular hero isn’t something that is conducive to being effective or efficient with your staff or your business. I saw this later when I came to Denver and a poorly designed system that needed patches or reboots every week overloaded me and my staff.

    Much of what we do as developers or operational staff is to support others. We provide them with systems to get their work done, whatever that work might be. We work hard, and we find solutions to challenges, but we ought to not only provide stability to customers, we need to do that over time. That means that we need to have a staff that we can count on over time. If we overload them or struggle to retain them because of burnout, we can’t easily do that. We also can’t get new, perhaps more important work done if we are constantly dealing with the same issues.

    I’ve seen management do this, and they end up with staff that isn’t efficient, can’t tackle new work, or turn over constantly. Some of you might know of a company like this in your area where they are always hiring because they aren’t a place anyone wants to work. Someone will work there because they need a job, but that company is never very efficient. Eventually, they’ll struggle with their competition and lose business. Or maybe not, maybe they’ll linger on with lots of missed opportunities.

    I’ve also seen IT professionals live like this and thrive on emergency situations. If you’ve read The Phoenix Project, you’re familiar with Brent, who is the go-to person for anything. I’ve been that person, and I’ve seen that person in different organizations. While I always appreciate them, I’m often frustrated because they become too busy to handle my requests and there is a constant stream of requests because things aren’t being fixed or solved. It’s never a good situation.

    We need to be heroes at times, but those ought to be emergencies, and they ought to be rare. Most IT professionals (and others) I know will work longer or harder when needed, but the need can’t be constant or even regular. It should be an unusual situation. If it’s not, then something has to change, at least for me. Either the technology or the staff. As I gained experience and savvy, this has usually meant I go find another job because the situation has become “emergencies are status quo.”

    Life’s too short to live like that.

    Steve Jones

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

  • Friday Flyway Tips–Seeing Pending Migrations

    I find that quite a few people using Flyway will end up with a lot of migration scripts over time. While you can certainly re-baseline and split scripts into separate folders, visualizing these over time can be hard.

    The Flyway Desktop team added a nice little option that makes it easier to see new work as opposed to old work.We’ll look at that in this post.

    I’ve been working with Flyway Desktop for work more and more as we transition from older SSMS plugins to the standalone tool. This series looks at some tips I’ve gotten along the way.

    Lots of Migration Scripts

    We might see a lot of migration scripts over time in a folder. Certainly I can see this in the file system for one of my projects.

    2023-10-19 15_06_07-migrations

    In Flyway Desktop,  here is my view.

    2023-10-19 15_40_08-Flyway Desktop

    That is a lot of scripts. Since these are ordered as they would apply, it can be a lot of scrolling to find the ones that haven’t been applied.

    However, if I click an environment on the right, I get a different view. Now I see a checkbox above the migrations that says “Only show pending migrations”.

    2023-10-19 15_40_29-Flyway Desktop

    If I click that, I see a view of the few that haven’t been applied to this environment.

    2023-10-19 15_42_13-Flyway Desktop

    A quick way to see what work has been added to the project, but not applied to other environments.

    Try it out today. If you haven’t worked with Flyway Desktop, download it today. There is a free version that organizes migrations and paid versions with many more features.

    Video Walkthrough

    I made a quick video showing this as well. You can watch it below, or check out all the Flyway videos I’ve added: