Search code examples
sqlsummaxhavinghaving-clause

SQL: Having MAX <> SUM


It's actually a very simple query, but I don't know why it's showing me values that are actually excluded with the HAVING.

Wrong Entries


Table 1 -> Maintable

  • ExpenditureComplete -> Float
  • ID

Table 2 -> Subtable (more then 1 Entry for Maintable)

  • DurationHour
  • MainTable_ID (link to ID of Table1)
  • ID
SELECT  
            a.ID, 
            b.MainTable_ID,
            MAX(a.ExpenditureComplete) as ExpenditureHours, 
            SUM(b.DurationHour) as DurationHours

    FROM [Table_1] as a
            Inner join
                [Table_2] as b
                    On
                        a.ID = b.MainTable_ID 

    Where a.Inactive = 0 
          and b.Inactive = 0

    Group by
                a.ID, 
                b.MainTable_ID

    Having MAX(a.ExpenditureComplete) <> SUM(b.DurationHour)

Solution

  • Try this:

    Having ROUND(MAX(a.ExpenditureComplete),2) <> ROUND(SUM(b.DurationHour),2)