3 ms·
A somewhat related tip that works in SqlServer (not sure about other dialects) is to put the column alias first rather than at the end of a calculated column. E
by joshka 11y ago
A somewhat related tip that works in SqlServer (not sure about other dialects) is to put the column alias first rather than at the end of a calculated column. E.g. rather than:
SELECT
Foo,
Sum(Baz) AS Bar
FROM Quux
Use instead:
SELECT
Foo,
Bar = Sum(Baz)
FROM Quux
The advantage here is that the names also line up neatly and the shape (column names etc.) of the result is obvious.
- chris_wot 11y agoIs that standard SQL?
- joshka 11y agoLikely not. My brief digging into trying to find a definitive document on what is 'standard SQL' led me down a rabbit hole and I gave up on working out whether it was or wasn't. The likelihood of having to change to a different provider on any project that has settled on SqlServer in my experience is fairly close to 0. YMMV
- rupert_murdaaa 11y agoI'm so used to using and reading the "AS" syntax for aliasing that if I came across this it'd throw me off. It seems to me that sticking everything in CTEs would be equally as jarring. Writing SQL or reading and understanding someone else's SQL is almost always a non-linear process, not least of all because the processing order by the database engine isn't top-to-bottom.
- joshka 11y agoIt took a minute for me to accept it as well, but once I did the queries just look so much better.