no capability to add ORDER BY table models
See original GitHub issuea user in dbt Slack asks:
Hi all, is there a way to specify “sort” column(s) or order by for models created in SQL Server? When I put in an order by clause I get the following error:
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server] The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
Because we’re using the SELECT * INTO DML for our model generation, if you include an ORDER BY in the SELECT query of a model, you’ll get the above error. But I’m confused because the docs say it should be allowed, but isn’t guaranteed…
When used with a SELECT…INTO or INSERT…SELECT statement to insert rows from another source, the ORDER BY clause does not guarantee the rows are inserted in the specified order.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
laravel - How to sort by a field of the pivot table of a many-to ...
I have no setup to try it out, but have you tried something like $songs = $usr->songs()->pivot()->order_by('play_count','desc')->songs(); ? Question is if you ...
Read more >Create a Data Model in Excel - Microsoft Support
A Data Model is a new approach for integrating data from multiple tables, effectively building a relational data source inside the Excel workbook....
Read more >Eloquent: Relationships - The PHP Framework For Web Artisans
Database tables are often related to one another. For example, a blog post may have many comments or an order could be related...
Read more >Read/write capacity mode - Amazon DynamoDB
Reserve provisioned throughput capacity for reads and writes when creating or updating a table in DynamoDB.
Read more >Model Meta options - Django documentation
Ordering is not a free operation. Each field you add to the ordering incurs a cost to your database. Each foreign key you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

There is only one scenario where
ORDER BYin anINSERTmakes sense, and that’s for guaranteeing thatIDENTITYcolumns are assigned values in order. In all other cases this does not do what you might want it to do – in particular, every time youSELECTfrom a table you must specify anORDER BYif you want an order, otherwise the engine is free to rearrange things as it sees fit (and it will, if the table is a columnstore, or parallelism is involved, or it can reuse a scan in progress from another query), so whatever order you think you may have gotten from the insert will not be useful anyway (and that’s before we consider rows being moved through deletes and updates). People often fall into the trap of thinking there’s a “natural physical order” that you’re guaranteed to get if you doSELECT *with noORDER BY, but there isn’t.So this is probably not useful to enable unless there really is a use case of building models with
IDENTITYcolumns (unlikely, sinceROW_NUMBER()gives you predictable values as well), and even then, as Mikael notes, you’d need to not use a view. (In olden daysSELECT TOP(100) PERCENT ... ORDER BYcould be used as a trick to circumvent this restriction, but while that’s still allowed syntactically, the optimizer simply pretends you didn’t write anORDER BYin that case.)As a minor quality of life improvement, you could consider allowing
ORDER BYin theSELECTso people have more convenient access to the results as they’re developing the model, but then strip out theORDER BYwhen it’s used for anINSERTsince it won’t do good anyway. But as this requires parsing the statement and is easy to get wrong, I’m not sure that’s worth the effort.Closing this, as it is not really feasible, and not desirable.