Author: way0utwest

  • Basic OFFSET–#SQLNewBlogger

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

    The other day I saw an article on the OFFSET clause in a SELECT. I had seen this come out and looked at it briefly in SQL Server 2012, but hadn’t done much with it.

    NOTE: if you use this, be sure you read about potential performance problems and solutions.

    The basic structure of this clause is that it is a part of the ORDER BY section of a query. After the column ordering, I can enter OFFSET and a value, which will skip those rows. I can optionally enter a number of rows to fetch.

    The structure is:

    <query>
    ORDER BY col1, col2
    OFFSET n ROWS FETCH NEXT 10 ROWS ONLY

    This code:

    WITH myTally(n)
    AS
    (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)
    )
    SELECT *
    FROM myTally
    ORDER BY n

    Will get me numbers from 1 to 100, each in a separate row. A tally table, with partial results shown in this image.

    2021-04-19 13_56_15-SQLQuery5.sql - ARISTOTLE.DMDemo_5_Prod (ARISTOTLE_Steve (61))_ - Microsoft SQL

    If I change this, and add an OFFSET, I can skip some rows. For example, I can skip 7 rows by adding that clause, as shown below.

    2021-04-19 13_58_58-SQLQuery5.sql - ARISTOTLE.DMDemo_5_Prod (ARISTOTLE_Steve (61))_ - Microsoft SQL

    If I only want a certain number, say 6 rows, I add the FETCH clause.

    2021-04-19 13_59_40-SQLQuery5.sql - ARISTOTLE.DMDemo_5_Prod (ARISTOTLE_Steve (61))_ - Microsoft SQL

    This is useful for pagination, saving some network bandwidth, and less buffer space on the client. Not necessarily helping the query processor, but it does make it easy for developers and with small result sets (and source table sizes), this is nice.

    It’s a fairly easy clause to use, but it can still require the full work on the server for looking through data, so be sure you read the link in the note above.

    SQLNewBlogger

    I was testing some code I’d seen from someone and it occurred to me to document the process a bit. I used a tally table, and wrote this around a couple of my experiments.

    You can do this as well, show some learning, testing, understanding of code in ten minutes.

  • Daily Coping 21 Apr 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 get active by singing today (even if you can’t)

    I can’t sing. My wife and kids say I do fine if we’re not actually trying to hit the notes. However, I love music. My mobile devices have been important for me as music players with other features being secondary. I still miss my iOS devices a bit as they were superior music players.

    Today, I went old school on Spotify, a Classic Rock Workout, playing loud in my office while I do a little busy work. I can semi-listen and sing along with a few songs as well. A few of the ones that get me going:

    • Gimme Shelter
    • Come Together
    • Layla
    • Baba O’Riley
    • Purple Haze

    You can guess the artists, or check out the playlist.

  • Daily Coping 20 Apr 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 relax today with some yoga, tai chi, meditation or a quiet walk.

    My choice is yoga. I try to do this 3-4 times a week, some in the gym, some at home. The gym is getting busier, so I’m doing a bit more at home. My usual choice is Yoga with Tim, who I’m enjoyed taking some medium level workouts from over the year. My wife got me started, and he has a few nice 30 day challenges to get you going.

    However, a friend got me started on Yoga with Adriene, which is more renewing, meditative, and relaxing to me. Not as hard a workout, but refreshing. I decided to pick up a video today and take it easy.

  • The Diverse Data Platform–Dativerse.io

    The other day I got a note from Ben Weissman the other day that he was running a new event,  http://dativerse.io/. Not much information, but I liked this list in the call for speakers.

    1. Your session must be related to the Microsoft Data Platform
    2. You contribute to the diversity of the Microsoft Data Platform

    I often talk about the first one, but the second? That’s interesting. I assume this means some sort of new talk that expands on the way we use the platform. New technology, new ways of putting things together, new something.

    Or maybe a diverse speaker, someone that isn’t one of the same speakers that are often at most events. It’s an interesting idea here.

    I don’t know if I’ll submit. While I can speak on the data platform, I don’t know I add much diversity in some ways. I do sometimes pick niche topics, but if I do something I’ve already done, is that adding to diversity?

    I’m not sure, but I’m thinking about it. The CFS closes on May 16, 2021, so I, and you, have time.

    Even if you don’t want to submit, perhaps you want to encourage someone else to submit, or even co-submit with you and help them deliver a session. We need to encourage and broaden our horizons, which is something that we all can work on.