Tag: syndicated

  • FIRST_VALUE vs. Min: #SQLNewBlogger

    I had mentioned some new T-SQL functions for SQL Server 2022 and a commenter asked about the difference between Min() and First_value. This post looks at a few cases.

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

    The Scenario

    Let’s set up some data and examine these functions.  First a table and some data:

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[MonthlySales](
         [TransactionDate] [date] NULL,
         [SalesAmount] [decimal](10, 2) NULL
    ) ON [PRIMARY]
    GO

    Some data for 2024:

    insert MonthlySales (transactiondate, SalesAmount)
      values
      ('2024-01-30', 100),
      ('2024-02-28', 200),
      ('2024-03-30', 300),
      ('2024-04-30', 400),
      ('2024-05-30', 500),
      ('2024-06-30', 600),
      ('2024-07-30', 700)

    Now, a query and some results. This query runs the same over() clause for first_value, min, and sum. The sum just shows totals.

    2024-11_0108

    It seems that first_value and min do the same thing. Let’s add one item to this. I’ll add two columns that remove the first_value and min from the total sales. Let’s assume we want to get rid of the first month’s sales for some reason (they lag).

    2024-11_0109

    OK, this looks good.

    The Difference

    Now, I’ll add some 2023 sales with this code:

    insert MonthlySales (transactiondate, SalesAmount)
      values
      ('2023-01-30', 10),
      ('2023-02-28', 20),
      ('2023-03-30', 5),
      ('2023-04-30', 40),
      ('2023-05-30', 50),
      ('2023-06-30', 60),
      ('2023-07-30', 70)

    When we re-run the query, we see different results for lines 3-7. This is because above, we had increasing sales in every line, when ordered by date. In 2023, we have a dip in sales, which happens, so the min value after Mar is 5, but the first_value is still 10 (from Jan).

    2024-11_0111The same sort of issue can come with last_value and max, as the ordering might not match the sorting of values.

    At first glance, these might seem like duplicate functions, but that really depends on the use cases you have for windowing functions and your data. You might often order by a numeric used in an aggregate, in which case they can be the same. However, there are plenty of cases where these work differently.

    Another quick example: in my baseball database we can see Barry Bonds HR counts by year, and the min differs from the first year in SFN.

    2024-11_0113

    SQL New Blogger

    This post took me about 15 minutes to setup and write. I’ve practiced telling a story, but I bet most of you could produce this in 30-45 minutes of work. Easy if you spend 1 hour a week on your career branding.

    Showcase your skills and set up a blog today.

  • A New Word: Nachlophobia

    nachlophobia – n. the fear that your deepest connections with people are ultimately pretty shallow, that although your relationships feel congenial in the moment, an audit of your life would reveal a smattering of low-interest holdings and uninvested windfall profits, which will indicate you were never really at risk of joy, sacrifice, or loss.

    I don’t have nachlophobia. Maybe in my younger years, but I deeply value connections with those I choose to spend time with. I have learned to make an effort at events to spend significant time with a few people, even if this means I don’t get time with others.

    I have no fear that I’m not risking anything; I am. I am truly saddened when my close friends experience pain and joyful for their success.

    A few weeks ago at the PASS Summit, I spent an hour sitting in a mostly empty room with a friend, talking about life. We chat periodically, but we don’t see each other enough. It was worth every minute. I also had to go offsite to an event and another friend asked to ride with me. I was grateful for the company and conversation, something I don’t get enough of.

    I hope none of you have nachlophobia. If you do, spend time building strong relationships, which are defined by you. It doesn’t matter how others feel about me, it’s about how I feel about them.

    From the Dictionary of Obscure Sorrows

  • Adding Test data to msdb.sysjobhistory

    One of our internal people was looking to test some process in (I assume) Redgate Monitor and needed more job history than they had in msdb.sysjobhistory. They wanted to use SQL Data Generator to help, but couldn’t make it work.

    This gives the solution I sketched for them. It can work with some other system tables, but not all. Many system tables do not allow user data to be inserted. Some do.

    The Main Problem

    The main problem here is that SQL Data Generator doesn’t see system tables. If I open a project in msdb, I see this:

    2024-11_0297

    That matches what I see in SSMS. Only user tables.

    2024-11_0296

    However, I know I can do this and it works.

    INSERT INTO dbo.sysjobhistory
      (job_id, step_id, step_name, sql_message_id, sql_severity, message, run_status, run_date, run_time, run_duration, operator_id_emailed, operator_id_netsent, operator_id_paged, retries_attempted, server)
    VALUES
      ('EA6B3BC3-D358-4B0C-A793-8C3C558098AB', 0, 'mystep', 0, 0, 'Executed as user: NT Service\SQLAgent$SQL2022. The job script encountered the following errors.'
      , 0, 20241111, 175438, 0, 0, 0, 0, 0, 'ARISTOTLE\SQL2022')

    Now, how to get SQL Data Generator to help?

    A Little System Table ETL

    Since I know I can insert data into the table, how can I generate data? Apparently SQL Data Generator cannot read these tables, but I can use a trick that I’ve used in the past.

    First, I’ll run this in msdb:

    SELECT *
      INTO mysysjobhistory
      FROM dbo.sysjobhistory AS s
      WHERE 1 = 0

    This code will make a copy of dbo.sysjobhistory with no data. However, this is a user table. Once I do this, now I can refresh SQL Data Generator and I can see my table.

    2024-11_0094

    Now I can use the settings to get the type of data I want. Here’s a preview of some data I set, using the data in my existing table.

    2024-11_0095

    Now, I can click generate data and I have data added to my user table. If I query this table, I see data:

    2024-11_0096

    The last step is to move this data. I’ll use this query:

    INSERT dbo.sysjobhistory
       (job_id, step_id, step_name, sql_message_id, sql_severity, message, run_status, run_date, run_time, run_duration, operator_id_emailed, operator_id_netsent, operator_id_paged, retries_attempted, server)
    SELECT job_id, step_id, step_name, sql_message_id, sql_severity, message, run_status, run_date, run_time, run_duration, operator_id_emailed, operator_id_netsent, operator_id_paged, retries_attempted, server
      FROM dbo.mysysjobhistory AS m

    Once this runs, I can see the data in msdb.

    2024-11_0097

    Of course, I’d also have to populate sysjobs if I wanted this linked to a job and shown in the Agent Job History Viewer.

    Summary

    This post showed how I’ve sometimes worked in situations where I couldn’t directly access a table from an application. In this case, I want to get data into sysjobhistory, but SQL Data Generator doesn’t support that directly. My solution:

    1. make a copy of the table
    2. insert data into the copy
    3. move the data into the original

    This has worked in a few situations as well where I might need to move/stage data before it gets into an application table. In this case, we wanted to generate some random history for Redgate Monitor to read.

    This can work for other tables as well, as long as you can insert..select into them.

    SQL Data Generator is a neat tool to generate data quickly for a variety of purposes in SQL Server. Give it a try, especially if you already have the Toolbelt Essentials.

  • Episode 11 of Simple Talks: Oracle

    The 11th episode is now live, recorded a few weeks ago at the PASS Data Community Summit. This was a lot of fun, us grabbing a space in the convention center and putting this together.

     

    Spotify: https://open.spotify.com/show/4sLQQR7DgYDPj2Ob1muIgU?si=e1589ae8d52947dd

    Also on Apple Podcasts