只有两个对象+休息
我想显示以下字符串:
苹果,香蕉,腰果,甜甜圈,大象,鱼
:
苹果,香蕉 + 4其他
我只想展示前两个对象并休息计数!
I want to show the below string:
Apple,Banana,Cashew,Doughnut,Elephant,Fish
into this format:
Apple, Banana + 4 others
I only want to show first two objects and rest the count !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就像其他人所说的那样,这不是SQL Server的工作,而是您的演示层。根据您的预期结果,我假设您的逗号意味着您正在数据库中存储划界数据;致命的缺陷。如果您使设计归一化,则很可能在应用程序层中很容易实现这一目标。
当您使用不规范数据时,您需要首先将其归一化,然后再重新审核它。我使用任意
top
(每次运行查询时行都可能有所不同),因为Azure SQL数据库中的序数参数仅(当前);希望序数参数将在SQL Server 2022中。无论如何,此 works ,但再次修复您的设计,并在应用程序层中执行此操作。如果您不在SQL Server 2017上,那么这是必须的,不是一个非常有力的建议。
This, like others have said, isn't a job to SQL Server, but your presentation layer. Based on your expected results, I assume that your commas mean you are storing delimited data in your database; a fatal flaw. If you normalised your design, you could likely easily achieve this in your application layer.
As you are using denormalised data, then you need to first normalise it, then reaggregate it too. I use an arbitrary
TOP
(the rows could be different every time you run the query), as the ordinal parameter is only (currently) available in Azure SQL Database; hopeful the ordinal parameter will be in SQL Server 2022.Anyway, this works, but again, fix your design, and do this in the application layer. If you aren't on SQL Server 2017, then that is a must, not a very strong suggestion.
就像其他人所说的那样,这不是SQL Server的工作,但是
如果您必须在SQL中进行的演示层,那么也许您可以这样做。
我确实假设您在多行中具有这些值,并且需要通过某些分组将它们串联,如果不是这样,则在您的问题中更清楚地
看一下 dbfiddle> dbfiddle在这里看到它的工作
结果看起来像这个
如果您不想显示“和X其他”,则可以更改此查询,例如此
(无列 >编辑不支持String_agg 的SQL Server,
以防您拥有不支持
string_agg
函数的SQL Server的较旧版本,则可以使用XML PATH < /code>串联值和
stuff
以删除Extra,
使用XML路径 dbfiddle
结果将再次相同
This, like others have said, isn't a job to SQL Server, but your presentation layer
If you must do it in sql, then maybe you can do it like this.
I do assume you have these values in multiple rows and need to concatenate them by some grouping, if not then make that more clear in your question
Look at this DBFiddle here to see it working
The result looks like this
If you don't want to show the 'and x others' you can alter the query like this
EDIT for sql server that does not support string_agg
In case you have an older version of sql server that does not supports the
string_agg
function, you can do it withXML PATH
to concatinate the values andstuff
to remove the extra,
DBFiddle using XML Path
The outcome will be the same again
我创建了这个,它对我有用。但是,它需要优化。
I have created this, and it works for me. However, it needs optimising.
可以用光标,通过使用函数 split_string 。
This task can be solved with the power of the cursor, by dividing the line by the delimenter using function split_string.