Author: way0utwest

  • Generating a Constrained Random Date–#SQLNewBlogger

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

    There have been lots of posts on the topic of generating random values, and some great articles. One of my favorites is Jeff Moden’s Generating Test Data: Part 1 – Generating Random Integers and Floats. Part 2 deals with dates, and that’s actually what I needed, but really I needed part 1.

    In my situation, I was helping a customer generate some random data. They had filled a table, Customers, with some data.

    2018-08-24 13_05_44-Microsoft Edge
    The goal was to populate a child table with some data. The child table had a date column that was supposed to be between the Entered and Exit dates in the Customer table.

    My update would have a join, obviously, and I can reference the enter and exit date, but how to get a date between them? My first thought was that I wanted a DATEADD() function. Something like this:

    UPDATE ce
    SET ce.EventTimeStamp = DATEADD( MINUTE, SomeRandomValue, c.CustomerExitedDateTime)), c.CustomerEnteredDateTime)
    FROM   dbo.Customer AS c
    INNER JOIN dbo.CustomerEvent AS ce
    ON ce.CustomerID = c.CustomerID

    The trick is what random value to use? If you look through Jeff’s article, you will see that the trick is to use a tally table and the NEWID() function. However, this doesn’t work:

     UPDATE ce
    SET ce.EventTimeStamp = DATEADD( MINUTE, NEWID(), c.CustomerEnteredDateTime)
    FROM   dbo.Customer AS c
    INNER JOIN dbo.CustomerEvent AS ce
    ON ce.CustomerID = c.CustomerID
    ;

    What I need to do is convert the GUID to a number. In this case, I added CHECKSUM around it, again, as in Jeff’s article. Then use ABS() to enclose this to get all positive numbers.

     UPDATE ce
    SET ce.EventTimeStamp = DATEADD( MINUTE, ABS(CHECKSUM((NEWID())))), c.CustomerEnteredDateTime)
    FROM   dbo.Customer AS c
    INNER JOIN dbo.CustomerEvent AS ce
    ON ce.CustomerID = c.CustomerID
    ;

    This gives me values, but they aren’t constrained. What I need to do is limit the upper random value so that the end time doesn’t exceed the Customer.CustomerExitDateTime for that row.

    To do this, I can constraint a large set of numbers to some value with the modulo function. This will limit what values can appear. The basic script is this:

    UPDATE ce
    SET ce.EventTimeStamp = DATEADD( MINUTE, ABS(CHECKSUM((NEWID())))) % 10, c.CustomerEnteredDateTime)
    FROM   dbo.Customer AS c
    INNER JOIN dbo.CustomerEvent AS ce
    ON ce.CustomerID = c.CustomerID
    ;

    This would give me values between 1 and 0 minutes after the start time, but this doesn’t mean these values won’t be after the exit time. This is also an unrealistic window if most of the time the enter and exit times vary by hours.

    What I did instead was to use the difference between the enter and exit times, with DATEDIFF() as my modulo function. That gives me:

    WITH myTally (n)
    AS
    -- SQL Prompt formatting off
    (SELECT n = ROW_NUMBER() OVER (ORDER BY (SELECT null))
      FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) a(n)
       CROSS JOIN (VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) b(n)
    )
    UPDATE ce
    SET ce.EventTimeStamp = DATEADD( MINUTE, ABS(CHECKSUM((NEWID()))) % (DATEDIFF(MINUTE, c.CustomerEnteredDateTime, c.CustomerExitedDateTime)), c.CustomerEnteredDateTime)
    FROM   dbo.Customer AS c
    INNER JOIN dbo.CustomerEvent AS ce
    ON ce.CustomerID = c.CustomerID
    ;

    I run this, and I get the table updated with a random set of values.

    2018-08-24 13_19_08-Microsoft Edge

    SQLNewBlogger

    This was a problem in my daily work. It was a customer, but it could easily be an internal query problem. I spent about 10 minutes grabbing screen shots and taking apart the query I’d built.

    You can do this, too. Show us your mind working with the solutions you write in your own blog.

  • Limiting the Ability to Concentrate and Collaborate

    I used to have an office. I guess I have an office now, but it’s not mine; it’s shared with my wife. As I look back at my career, I realize that I’ve often taken a job that switched me from a more open working environment to an office, and back. Early in my career, we had cubes, which a few of us combined into an open 4 desk space at one point. Then I took a job with an office, then back to an open plan (10 developers in a room), then my own office, before ending up with my shared office. Actually, I’m always sharing a space. When I travel to the Redgate offices, they have an open plan, so I get a desk that’s open to a few dozen other people.

    I’ve gone back and forth on whether or not I want a shared space. I’ve come to the conclusion that it really depends on my job. When I worked as a sysadmin, where we were constantly collaborating with others, an open, shared space worked well. We could easily talk to each other, which was an efficient way to work.

    When I’ve worked as a developer or a manager, I’ve preferred having my own office. The ability to concentrate for longer periods of time is helpful for me, and while headphones separate me, they aren’t as private, as there can still be visual distractions as people move around. I’ve had people come up to tap me on the shoulder, which can ruin my concentration at times.

    There’s an article at Ars Technica that talks about a study showing that open plans result in much less productivity and even less interaction. That seems counterintuitive, as I’d expect an open plan to encourage more interaction. Indeed, when I’m at the Redgate office, I find that someone will often turn and chat with a friend. Or if two people are talking, it can become a watercooler atmosphere where others join in. There seem to be plenty of interaction.

    When I’ve been at Microsoft, where everyone has a small office and most with a window to the hallway, I find lots of open and closed doors, with the former inviting others in and the latter discouraging it. I do see plenty of workers get distracted if someone stops and looks in the window. At the same time, I also find there are lots of open spaces where people can engage with others and even work in an open space if they desire.

    I don’t know if one way is better or worse than the other. I know that I feel differently at different times about how I work. At Redgate, we do have some pods for private work if you choose, but they aren’t completely closed off. They’re more like a cube or a small space for one person. I do find that having a private space is important for me at times, and if I were to go back to a company’s location on a regular basis, I’d want to ensure I had some sort of private office, even if it were only for a portion of a week and others used it when I wasn’t there.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 5.1MB) podcast or subscribe to the feed at iTunes and Libsyn.

  • Bringing DevOps to #SQLSat Oslo

    2018-08-16 12_50_39-SQLSaturday #746 - Oslo 2018 _ Event Home

    As my kids get older and I spend less time with them, I look forward to visiting new countries. While I’ve been to Norway and Oslo before, I wasn’t able to make the SQL Saturday last year. I am lucky that they accepted my submission and I’m heading  SQL Saturday #746 – Oslo in a week.

    There’s a great schedule with some amazing sessions to watch. I’m on it with my Bringing DevOps to the Database talk, showing you the process and idea behind adding your database development to an application DevOps pipeline.

    I’m also looking to try and get to Oslo early Friday and spend a little time hiking somewhere and enjoying the beautiful space. It’s on my list to travel up the West side of the country some day, and even get to Svalbard, but that will have to wait for a less busy time when my wife can join me.

    If you’re near Oslo, come to an amazing event and enjoy a little time in a little city by the sea.

  • Fun at Work

    I am lucky in that I get to travel and speak at a number of events every year. As a result, I meet lots of people in a variety of situations. Many of these places have a social component and often result in pictures. I love that because pictures are memories for me, and I look back at them to reminisce about good times in life.  I greatly appreciate having Facebook remind me of images from the past, some of which I’ll share back out as they’re great memories for others in my family.

    This has changed dramatically in the last decade, as everyone seems to have a camera on them at all times in their phone. That means many more memories I can capture. Some of these are silly, and I wrote a blog with some of the funny ones I’ve gotten over the years. In the past, I have relatively few pictures from work. Early in my career, film cameras were rare. We only carried them around for special events, which wasn’t often at work. I do have actual physical pictures of a few work parties, but rarely of workspaces and situations. There are certainly some I wish I’d been able to capture and save.

    For many of you, I’m sure that’s different now. Some of you have grown up with cameras and spent most of your career with them. When I asked about desks, quite a few of you posted pictures, with some really interesting items. Those are likely going to be fun memories for you at some point in the future.

    Today I’m wondering if you have some funny, silly, even head shake photos of yourself or work. Nothing that will get anyone in trouble or be offensive, but maybe you’ve run across a sign on a wall, some wild cabling, or even a snapshot of you and others celebrating some accomplishment. If you can share them. Check out the fun pics on my blog over the years that make me smile, and if any of you have old pictures of me, please post or send them along.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 3.1MB) podcast or subscribe to the feed at iTunes and Libsyn.