Moderately frequently, I find myself doing a grouping, that I know will result in the whole group having the same value in a particular column, but SQL Server doesn't know that.
Most often, it's that I've grouped by DATEPART(Month, my_date_column) and then I want to SELECT DATEPART(Year, my_date_column) where all the data is in a single year or SELECT DATENAME(Month, my_date_column)
SQL Server doesn't know that these are implicitly all identical, so I end up using MIN() or MAX().
This works, but it feels wrong. (And misleading for future developers!)
Is there a SINGLE() function or anything comparable?
Ideally it would error if they weren't all unique, but I'd taking anything that was more explicit about what I was doing.
SINGLEis the misleading function. What wouldSINGLEdo if it encountered a different value? It can't throw random errors. It could only work if somehow you ensured that all results were identical, as if you called DISTINCT on them. That's not how aggregates are expected to work