I have a table that contains the following columns:
ID int, DISTANCE float, EVENT Varchar
What i would like to acheive is to select all the data, but to group by the event and distance fields (i.e. to remove duplicate events at the same distance).
The issue I can see is that the Distance column is a float, and as such the GROUP BY may not behave expectantly. Most (i say most) of the data is stored to 3 decimal places, and this is the criteria I would like to group on.
Example Data:
ID,Distance,Event
1, 0.001, A
2, 0.002, A
3, 0.002, A
4, 0.002, B
5, 0.003, C
6, 0.0035,C
So the result would look like:
1, 0.001, A
2, 0.002, A
4, 0.002, B
5, 0.003, C
You can use
CAST()to convert the decimal to string and only use the first 5 characters of the converted string ("x.xxx"), which you can then group on withGROUP BY:Ex:
EDIT: You can also use
TRUNCATE()on theDistancefield which may actually be better in terms of performance:Additional info on CAST() and TRUNCATE()