The previous query with your data.
SELECT Cast(Cast(( CASE WHEN washes = 0 THEN washes ELSE washes - 1 END ) / 10 AS SMALLINT) * 10 + 1 AS VARCHAR(10)) + '-' + Cast(Cast(( CASE WHEN washes = 0 THEN washes ELSE washes - 1 END ) / 10 AS SMALLINT) * 10 + 10 AS VARCHAR(10)) grouping_descr,
material,
Cast(( CASE
WHEN washes = 0
THEN washes
ELSE washes - 1
END ) / 10 AS SMALLINT) AS grouping,
Count(washes) AS counter
FROM (SELECT 110011 AS rfid,
'Towel' AS material,
20 AS washes
UNION
SELECT 110122 AS rfid,
'Towel' AS material,
15 AS washes
UNION
SELECT 110511 AS rfid,
'Towel' AS material,
16 AS washes
UNION
SELECT 210121 AS rfid,
'Sheet' AS material,
11 AS washes
UNION
SELECT 110013 AS rfid,
'Towel' AS material,
5 AS washes
UNION
SELECT 110018 AS rfid,
'Towel' AS material,
6 AS washes
UNION
SELECT 210122 AS rfid,
'Sheet' AS material,
3 AS washes
UNION
SELECT 210155 AS rfid,
'Sheet' AS material,
12 AS washes) AS v1
GROUP BY material,
Cast(( CASE
WHEN washes = 0
THEN washes
ELSE washes - 1
END ) / 10 AS SMALLINT)
ORDER BY material
If the column names are the same you would replace what is in the from with your table name. Of course depending on the sql server you use you may need to adjust the syntax.
These is the result I get:
grouping_descr |
material |
grouping |
counter |
1-10 |
Sheet |
0 |
1 |
11-20 |
Sheet |
1 |
2 |
1-10 |
Towel |
0 |
2 |
11-20 |
Towel |
1 |
3 |
I made some modifications to conform with grouping in ranges of 1-10 instead of 0-9.
Andreas.
int(washes / 10)