2

I have two ms sql server tables with column of type float (not null).

when I do union ALL in those two tables

SELECT [float_column], 
       Min(sourcename) AS ExistsInFile 
FROM   (SELECT [float_column], 
               'File 1' AS SourceName 
        FROM   table1 
        WHERE  column1 = 'abc' 
        UNION ALL 
        SELECT [float_column], 
               'File 2' AS SourceName 
        FROM   table2 
        WHERE  column1 = 'abc') x 
GROUP  BY [float_column] 
HAVING Count(DISTINCT sourcename) <> 2 

it gave me following output, but it should not show any difference as both values are same.

float_Column     |    ExistsInFile
-53590187.62            File 2
-53590187.62            File 1

Is this because of float type? Any help would be appreciated!

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Relativity
  • 6,690
  • 22
  • 78
  • 128
  • 1
    Floats are approximate, they don't always store an exact value. This is why you should not rely on comparing them in a group by or where clause. – Tim Lehner Jul 08 '16 at 21:17
  • 1
    More info on using these datatypes in SQL Server: [Using decimal, float, and real](https://msdn.microsoft.com/en-us/library/ms187912.aspx) – Tim Lehner Jul 08 '16 at 21:24

1 Answers1

3

Yes, the problem is the float. The issue here is not the union all. The issue is the group by.

Floating point numbers can differ the the smallest bits representing the number -- and you can't see the differences. So, for your problem, just convert everything to a decimal:

SELECT CAST([float_column] as DECIMAL(12, 2), 
       Min(sourcename) AS ExistsInFile 
FROM   (SELECT [float_column], 
               'File 1' AS SourceName 
        FROM   table1 
        WHERE  column1 = 'abc' 
        UNION ALL 
        SELECT [float_column], 
               'File 2' AS SourceName 
        FROM   table2 
        WHERE  column1 = 'abc') x 
GROUP BY CAST([float_column] as DECIMAL(12, 2) 
HAVING Count(DISTINCT sourcename) <> 2 ;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786