Windowing without a partition by or an order by

Kenneth Fisher, 2018-02-21

If you’ve never worked with windowing functions they look something like this:

SELECT name, max(create_date) OVER 
		-- No this isn't meant to make sense
		(PARTITION BY lower(left(name,1)) ORDER BY owner_sid)
FROM sys.databases;

The other day someone mentioned that you could use ROW_NUMBER which requires the OVER clause without either the PARTITION BY or the ORDER BY parts. So let’s try that out.

-- Create a table to test with
CREATE TABLE Windowing (col1 int, col2 int);
INSERT INTO Windowing 
	VALUES (1,1), (1,2), (1,3), (1,4), (1,5)
		,(2,6), (2,7), (2,8), (2,9), (2,10);
GO
-- Test ROW_NUMBER without PARTITION BY or ORDER BY
SELECT ROW_NUMBER() OVER (), *
FROM Windowing;
Msg 4112, Level 15, State 1, Line 16

The function ‘ROW_NUMBER’ must have an OVER clause with ORDER BY.

 

So nope, doesn’t work. But can we get around it?

SELECT ROW_NUMBER() OVER (ORDER BY 1), *
FROM Windowing; 
GO
SELECT ROW_NUMBER() OVER (ORDER BY '1'), *
FROM Windowing; 
GO
SELECT ROW_NUMBER() OVER (ORDER BY DB_ID()), *
FROM Windowing; 
GO

Ok, so you can do it, but it takes some work. No integers, no constants at all, but a function that returns a constant value will do it. Not really sure why you would want to do this, since generally in order for a row number to be useful its done in order of some value. On the other hand, if you absolutely NEED a unique number value per row, don’t have a useful indexed column (a staging table that’s a heap maybe?), and you don’t want to pay for an expensive sort, this could be handy.

Let’s try one more for fun. MAX

SELECT col1, MAX(col2) OVER ()
FROM Windowing;

Now that works! However, now it doesn’t work with a GROUP BY making it less useful.

SELECT col1, MAX(col2) OVER ()
FROM Windowing
GROUP BY col1;
Msg 8120, Level 16, State 1, Line 29

Column ‘Windowing.col2’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

 

Unlike a simple MAX of course.

SELECT col1, MAX(col2)
FROM Windowing
GROUP BY col1;

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis

2009-02-23

1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren

2009-02-17

1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren

2009-02-13

360 reads