2

I'm trying to group together values in a CASE statement in SQL Server. When the values are positive, it works fine. When the values are negative, it blows up.

Here's a link to the SQL Fiddle I created to replicate it:

Here is the code:

    -- Create the table --

    CREATE TABLE Test (
        ClientID varchar(12),
        Value decimal(12,10)
      PRIMARY KEY (ClientID)
      );


    -- Load the data -- 
    INSERT INTO Test (ClientId, Value)
            VALUES('1','10'),
            ('2','-10'),
            ('3','11.2'),
            ('4','-11.6'),
            ('5','12.544'),
            ('6','-13.456'),
            ('7','14.04928'),
            ('8','-15.6089'),
            ('9','15.7351'),
            ('10','-18.1063'),
            ('11','17.6234'),
            ('12','-21.0034'),
            ('13','19.7382'),
            ('14','-24.3639'),
            ('15','22.1068'),
            ('16','-28.2621'),
            ('17','24.7596'),
            ('18','-32.7841'),
            ('19','27.7307'),
            ('20','-38.0296'),
            ('21','31.0584'),
            ('22','-44.1141'),
            ('23','34.785499'),
            ('24','-51.1726'),
            ('25','38.9597'),
            ('26','-59.3602'),
            ('27','43.6349'),
            ('28','-68.8579'),
            ('29','48.8711'),
            ('30','-79.8751');

Now to run the CASE statements:

SELECT 
  CASE
    WHEN Value BETWEEN 0 AND 20 THEN '0-20'
    WHEN Value BETWEEN 21 AND 40 THEN '20-40'
    ELSE '40+'
    END as ValueRange, count(*) as count
FROM Test
WHERE Value > 0
GROUP BY CASE
  WHEN Value BETWEEN 0 AND 20 THEN '0-20'
  WHEN Value BETWEEN 21 AND 40 THEN '20-40'
  ELSE '40+'
  END
;

That works fine. Results:

VALUERANGE      count
0-20           7
20-40          6
40+            2

Now with negative values - it doesn't work:

SELECT 
  CASE
    WHEN Value BETWEEN 0 AND -20 THEN '0-20'
    WHEN Value BETWEEN -21 AND -40 THEN '20-40'
    ELSE '40+'
    END
 as ValueRange, count(*) as count
FROM Test
WHERE Value < 0
GROUP BY CASE
  WHEN Value BETWEEN 0 AND -20 THEN '0-20'
  WHEN Value BETWEEN -21 AND -40 THEN '20-40'
  ELSE '40+'
  END
;

Result:

VALUERANGE  count
40+          15

It groups everythin as 40+ - not breaking down the lower valueranges.

Can anyone see what I'm doing wrong?

4

2 に答える 2

3

Try

SELECT 
  CASE
    WHEN Value BETWEEN -20 AND 0 THEN '0-20'
    WHEN Value BETWEEN -40 AND -21 THEN '20-40'
ELSE '40+'
END
 as ValueRange, count(*)
FROM Test
--WHERE Value > 0
GROUP BY CASE
  WHEN Value BETWEEN -20 AND 0 THEN '0-20'
  WHEN Value BETWEEN -40 AND -21 THEN '20-40'
  ELSE '40+'
END
;

I expect the between takes the smallest value first.

ValueRange 
---------- -----------
0-20       5
20-40      5
40+        20
于 2012-07-12T22:43:20.403 に答える
3

Your problem is already solved (put the negative number first so the BETWEEN values are in ascending order) but you might like to see some alternate query syntax (which I prefer to CASE constructions like you have):

SELECT 
   ValueRange =
      Convert(varchar(11), R.Low)
      + Coalesce(' - ' + Convert(varchar(11), R.High - 1), '+'),
   Qty = Count(*)
FROM
   dbo.Test T
   INNER JOIN (
      SELECT 0, 21
      UNION ALL SELECT 21, 41
      UNION ALL SELECT 41, NULL
   ) R (Low, High)
      ON T.Value >= R.Low
      AND T.Value < Coalesce(R.High, 2147483647)
WHERE T.Value >= 0
GROUP BY R.Low, R.High;

I prefer this because I find it easier to modify the range values than working with a case statement, plus it helps avoid repeating the numbers in the text description.

If you just want every 20 in a block then this is easier:

SELECT
   BlockHigh = (Value + 19) / 20 * 20, -- 20 for 1-20, 40 for 21 - 40, etc.
   Qty = Count(*)
FROM dbo.Test
GROUP BY (Value + 19) / 20 * 20;

I'm assuming in the first query that you might not be using integers, and in the second query that you are.

Note that your "40+" is wrong as it is really "41+" the way you've written the CASE statement.

于 2012-07-12T23:11:57.203 に答える