[英]Count maximum length of value in SQL Server
This is small sample of my salesDetail
table and its [Kind of business]
column:这是我的salesDetail
表及其[Kind of business]
列的小样本:
kind of business
-------------------------------------
Retail and food services sales, total ………………………………………...…………………………………………………………………………………………………
Total (excl. motor vehicle and parts dealers) ………………………………………...…………………………………………………………………………………………………
I want to separate all data by white space and then calculate maximum length of separated string.我想用空格分隔所有数据,然后计算分隔字符串的最大长度。 I tried this code我试过这段代码
SELECT MAX(LEN(value))
FROM salesDetail
CROSS APPLY STRING_SPLIT([kind of business], ' ')
GROUP BY value
This returns the length of individual string not maximum of all string.这将返回单个字符串的长度,而不是所有字符串的最大值。
The results are shown in the screenshot below.结果显示在下面的屏幕截图中。
So then I tried this code:所以我尝试了这段代码:
SELECT MAX(COUNT(value))
FROM salesDetail
CROSS APPLY STRING_SPLIT([kind of business], ' ')
GROUP BY value
This one throws an error:这会引发错误:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.无法对包含聚合或子查询的表达式执行聚合函数。
Expected output:预期输出:
147 (this is maximum number in this column)
I want maximum value from the result as shown in picture我想要结果中的最大值,如图所示
How should I fix this?我应该如何解决这个问题?
Simply remove the final GROUP BY
只需删除最后的GROUP BY
select max(len(value)) max_len
from salesDetail
CROSS APPLY STRING_SPLIT([kind of business], ' ')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.