简体   繁体   English

在SSRS 2008中排序字符串数值

[英]sorting string numeric values in SSRS 2008

I have a varchar field (i am grouping on) in a dataset that returns a list of values : 20, 25, 40, 100, 110, 'N/A'.. 我在一个返回值列表的数据集中有一个varchar字段(我正在分组):20,25,40,100,110,'N / A'..

I want the "numeric" values sorted from low to high : ie : 20, 25...110, 'N/A' 我希望“数字”值从低到高排序:即:20,25 ... 110,'N / A'

the problem is that the A>Z sorting in the grouping gives out the following output : 问题是分组中的A> Z排序给出以下输出:

100, 110, 25, ..., N/A 100,110,25,...,N / A.

I cannot convert to numeric datatype since there are string values.. 我无法转换为numeric数据类型,因为有字符串值..

Does anyone have a solution please ? 有人有解决方案吗?

Thank you in advance Jam 提前谢谢Jam

There are several solutions you can implement here. 您可以在此处实施多种解决方案。 I'll discuss the two I consider to be the easiest. 我将讨论我认为最简单的两个。 If these don't work for you, let me know because there are a multitude of options. 如果这些对你不起作用,请告诉我,因为有很多选择。 To make it easy, I've named the field you're referring to in your question as *num_text*. 为了方便起见,我将您在问题中引用的字段命名为* num_text *。

Fix in SSRS: Go to the Tablix Properties for the tablix displaying the data in question. 修复SSRS:转到显示相关数据的Tablix的Tablix属性。 Go to Sorting tab. 转到“排序”选项卡。 Click Add. 单击添加。 In the "Sort by" field, click the expression button and type the following: 在“排序依据”字段中,单击表达式按钮并键入以下内容:

=CInt(IIF(Fields!num_text.value = "N/A",9999999,Fields!num_text.value))

Note, you need to convert any possible text values to a number greater than any possible integer/decimal. 注意,您需要将任何可能的文本值转换为大于任何可能的整数/小数的数字。 In this case I chose 9999999 based on the examples in your question. 在这种情况下,我根据您问题中的示例选择了9999999。 If you have multiple text values that are not converted to number, Report Builder/BIDS will allow you to save the report, but when you render it, it will show #Error , signifying that the CInt (or any other conversion formula you choose) failed on a non-numeric value. 如果您有多个未转换为数字的文本值,则报表生成器/ BIDS将允许您保存报表,但在呈现时,它将显示#Error ,表示CInt(或您选择的任何其他转换公式)在非数字值上失败。

Fix in SQL: Add a new field (like field_sort) with datatype numeric and use your case statement generating the current field in question saying: 修复SQL:添加数据类型为numeric的新字段(如field_sort)并使用case语句生成当前有问题的字段说:

, Case
    When --Criteria leading to "N/A"
        Then 9999999
        Else num_text
    End as field_sort

--Rest of SQL Script here

Order by field_sort

Then just display your num_text field in SSRS, and the values will be sorted properly. 然后只需在SSRS中显示num_text字段,这些值就会被正确排序。 If you have many possible string values, then you might find it easier to fix in SQL (rather than specifying numerous IIF statements in SSRS. 如果您有许多可能的字符串值,那么您可能会发现在SQL中更容易修复(而不是在SSRS中指定大量的IIF语句)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM