如何编写sql语法来获取大于和小于给定数据的值?

时间:2016-09-29 14:08:13

标签: sql-server

我在sql table列中有一个数据,如下所示

Range
1000000+
2000000+
100001-250000
10001-11000
10001-12500
1001-2000
1001-2500
11001-12000
12001-13000
12501-15000
13001-14000
14001-15000
15001-16000
15001-20000
15001-25000

我需要在excel中使用以下条件显示两个值作为列

范围大于 - 6000然后计数(ID)为Greater_Range

的情况

范围小于 - 6000然后计数(ID)为Lesser_Range

的情况

我做了案例陈述,但与手动计算时相比,结果没有显示正确的ID计数。请有人建议我哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,你基本上想要确定大小大于6000的范围数量的计数,以及大小小于6000的范围。

您可以创建一个CTE,将范围字符串拆分为最小值和最大值。接下来,使用case语句确定范围的大小是否实际上大于或小于6000,只返回0或1(IsLessThan6000和IsGreaterThan6000列)

最后一步是将IsLessThan6000和IsGreaterThan6000列相加。以下示例演示了如何:

json_stream << time_.time_since_epoch().count();

答案 1 :(得分:0)

DECLARE @TerribleRangeTable TABLE
(
    [ID] INT IDENTITY PRIMARY KEY
    ,[Range]    NVARCHAR(100)
)

INSERT INTO @TerribleRangeTable 
(
    [Range]
)
VALUES  
        ('1000000+'),
        ('2000000+'),
        ('100001-250000'),
        ('10001-11000'),
        ('10001-12500'),
        ('1001-2000'),
        ('1001-2500'),
        ('11001-12000'),
        ('12001-13000'),
        ('12501-15000'),
        ('13001-14000'),
        ('14001-15000'),
        ('15001-16000'),
        ('15001-20000'),
        ('15001-25000')


DECLARE @maxUnbound INT = 1073741824;

WITH CTE_RangeMinMax
AS
(   
    SELECT  [ID]
            ,[Range]
            ,(  -- CASE statement 
                CASE    
                    WHEN CHARINDEX('-', [Range]) = 0 THEN CAST(REPLACE([Range], '+', '') AS FLOAT) -- If no ‘-‘ character is available then assume this is an unbound range on maximum. Simply replace ‘+’ character with an empty string and convert to float.
                    WHEN CHARINDEX('-', [Range]) > 0 THEN LEFT([Range], CHARINDEX('-', [Range]) - 1) -- Get the text left of ‘-‘character to get range minimum and convert to float. 
                    ELSE NULL
                 END
             ) RangeMin
            ,(
                CASE    
                    WHEN CHARINDEX('-', [Range]) = 0 THEN @maxUnbound -- If no ‘-‘ character is available then assume this is an unbound range on maximum. Set value to a large integer.
                    WHEN CHARINDEX('-', [Range]) > 0 THEN RIGHT([Range], LEN([Range]) - CHARINDEX('-', [Range]) ) -- -- Get the text right of ‘-‘character to get range maximum and convert to float. 
                    ELSE NULL
                 END
             ) RangeMax
    FROM    @TerribleRangeTable
), CTE_RangeGreaterOrLessThan
AS
(
    SELECT  [ID]
            ,[Range]
            ,RangeMin
            ,RangeMax
            ,(
                CASE 
                    WHEN (RangeMax - RangeMin) <= 6000 THEN 1
                    ELSE 0
                END 
             ) AS IsLessThan6000
            ,(
                CASE 
                    WHEN (RangeMax - RangeMin) > 6000 THEN 1
                    ELSE 0
                END 
             ) AS IsGreaterThan6000

    FROM    CTE_RangeMinMax
)
SELECT  SUM(IsLessThan6000) AS Lesser_RangeCount
        ,SUM(IsGreaterThan6000) AS Greater_RangeCount
FROM    CTE_RangeGreaterOrLessThan

获取最大值并进行比较