T-SQL Tuesday #168–Using Window Functions

tsqltuesdayI am the host for T-SQL Tuesday this month, and I hope that a lot of people like the topic. This idea actually came to me earlier this year when I happened to see someone ask about a T-SQL problem and get an answer using a Window function. This person mentioned they hadn’t used the window function before, and I wondered how many people haven’t even tried using the OVER() clause with a window function.

I also saved the idea of window functions just in case I didn’t have a host, and I realized a few months ago November was blank. So I created an invitation for technical solutions using window functions, hopefully mature solutions you’ve use many times.

If you want to host, contact me, and send me your blog link and I’ll get you scheduled. FYI, I’m looking for people in the second half of 2024, so it’s not an immediate need.

Cleaning Up Window Functions

I don’t write a ton of code, and I don’t have any really cool solutions, but I did want to highlight one thing from SQL Server 2022: the WINDOW clause.

In the past we’ve often had code like this:

WITH    HRCTE
           AS ( SELECT   hrorder = ROW_NUMBER() OVER ( PARTITION BY p.franchName ORDER BY HR DESC )
                       , p.nameFirst
                       , p.nameLast
                       , p.franchName
                       , p.HR
                FROM     dbo.Players p
              )
     SELECT  hrdenserank = DENSE_RANK() OVER ( PARTITION BY HRCTE.franchName ORDER BY HR DESC )
           , hrrank = RANK() OVER ( PARTITION BY HRCTE.franchName ORDER BY HR DESC )
           , playercound = COUNT(p.nameLast) OVER ( PARTITION BY HRCTE.franchName ORDER BY HR DESC )
           , hrsum = sum(p.HR) OVER ( PARTITION BY HRCTE.franchName ORDER BY HR DESC )
     FROM    HRCTE
     WHERE   HRCTE.hrorder <= 5;

That’s not bad, but I’ve written a bunch of repeating code in the OVER() clauses. In SQL Server 2022, I can do something more like this (just the outer query):

    SELECT  hrdenserank = DENSE_RANK() OVER frname
           , hrrank = RANK() OVER frname
           , playercound = COUNT(p.nameLast) OVER frname
           , hrsum = sum(p.HR) OVER frname
     FROM    HRCTE
     WHERE   HRCTE.hrorder <= 5
     WINDOW frname AS ( PARTITION BY HRCTE.franchName ORDER BY HR DESC );

That, to me, is a cool maturity of the Windowing function capability in T-SQL. I can alias a window and reuse it in my code. This also makes I can make a few different windows and easily see which one is used with which aggregate.

Posted in Blog | Tagged , , , | 1 Comment

Data Community Summit Week

I’m up in Seattle this week for the PASS Data Community Summit 2023. This is almost an annual event for me. I’ve missed a few since 1999, but not many.

This is my last trip of the year. It’s been quite a year of travel, and I’m glad not to have anything booked in the foreseeable future. I have one conference in late January, but I haven’t submitted for others or planned anything else for now.

In any case, I have a DevOps in a Day tomorrow and then a few panels at the Summit, but mostly I’m free to enjoy the time with people. I am hoping to do some livestreaming at the event, but we’ll see if that works. If they give me access to the Summit channels Winking smile

We’ll see if I can get some time to post pictures and thoughts here as well.

Posted in Blog | Tagged , , | 4 Comments

Republish: Gigging for a Career

I am in Seattle today attending the PASS Data Community Summit 2023, so you get Gigging for a Career as a republish.

Posted in Editorial | Tagged | Comments Off on Republish: Gigging for a Career

No Degree Needed

For most of my career, I’ve seen many companies that hire white-collar workers, including their IT staff, require a college degree or the equivalent experience in the military. Often this has been something the Human Resources department has made a requirement in their job descriptions and requests to recruiters. This was a filter that stopped many talented people from even getting an interview.

That hasn’t stopped a lot of people from pursuing careers as developers or IT staffers, and I’ve had many friends who have had successful careers without a degree.

The world has been changing, especially since the pandemic, and many companies are no longer requiring any sort of university degree for candidates. There are still a lot of job descriptions that “prefer” a degree, but even that is changing rapidly. I hope at some point that we stop looking at degrees as anything other than a bit of experience in working through a project and not as any sort of qualification for a particular job.

This week there was an article about seven ways to become a software engineer without a degree. The ways are: learn to program, earn a cert, contribute to an OSS codebase, write documentation, find employers who don’t care, be a freelance developer, and go to a bootcamp. Of these, a few seem redundant learning to code is probably needed to contribute to OSS or freelancing.

Of these, I overall like the advice. Being self-motivated or driven is a skill that many employers appreciate. Showing that you are producing something useful in the world is a good way to create opportunities. It’s also a sign that you’ll work to be productive and not expect to avoid work after getting hired. I know I prefer people who go figure things out rather than those who wait for someone else to tell them what to do. I don’t recommend bootcamps, however, unless you are very motivated to take whatever you learn and then expand on it with OSS contributions or other tasks that showcase and expand knowledge.

Finding a job is challenging at times, and certainly impressing someone enough to hire you is a task. A degree can help, especially with those who might be prejudiced towards university experience, but more and more people recognize that college doesn’t necessarily prepare you to be productive or a great employee. You can prove that to people yourself with some work, some documenting your efforts, and some good soft skills to explain what you know.

Steve Jones

Posted in Editorial | Tagged , | 2 Comments