在 SQL Server 中根据频率对视图进行排序 [英] Sorting the view based on frequency in SQL Server

查看:36
本文介绍了在 SQL Server 中根据频率对视图进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 stock_Outward 生成的 StockinHand 视图 &Stock_Inward 表格现在需要根据频率进行排序,即大多数移动的库存项目应该在表格的顶部

I have a StockinHand view generated from stock_Outward & Stock_Inward tables right now needs the sorting based on frequency i.e most moving stock items should be on top of the table

我的表格如下:

tbl_StockInward:

ID, Stock_Code,Units,Rate, Description, Vendor, DateOfPurchase, DateOfUpdate, Purchased_By, WareHouse, Remarks, 

反之tbl_StockOutward

请帮帮我

提前致谢

推荐答案

就像在子查询中一样,你不能在 sql server 的视图定义中使用 ORDER BY 除非你也使用 顶部.

Just like in sub queries, you can't use ORDER BY in a view definition in sql server unless you also use TOP.

这样做的原因是,视图的作用就像它们的表一样,而 sql server 中的表(实际上,在任何关系数据库中)都被认为是非有序集.
就像存储在表中的记录顺序没有意义一样,视图获取记录的顺序也没有意义.
您可以使用肮脏的黑客并编写 SELECT TOP 100 PERCENT ... 然后使用 ORDER BY,但我怀疑它是否有任何意义.

The reason for this is that Views are acted upon as if they where tables, and tables in sql server (in fact, in any relational database) are considered as not ordered sets.
Just like there is no meaning to the order of records stored in a table, there is also no meaning to the order of records fetched by a view.
You can use a dirty hack and write SELECT TOP 100 PERCENT ... and then use ORDER BY, but I doubt if it has any meaning at all.

H说了这么多,您当然可以在任何从视图中进行选择的查询中使用 ORDER BY.

Having said all that, you can of course use ORDER BY in any query that selects from a view.

这篇关于在 SQL Server 中根据频率对视图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆