I have an absolutely baffling case in front of me. I have two database environments, A and B, where environment B has been created from a backup of A. I'm running a rather simple query:
SELECT
customers.customerName (* varchar(100), not null *)
,countries.countryName (* varchar(100), not null *)
,Balance = Sum(invoices.Amount) (* money, not null *)
FROM invoices
INNER JOIN customers
ON customers.customerID = invoices.customerID
INNER JOIN countries
ON countries.countryID = customers.countryID
GROUP BY
customers.customerName
,countries.countryName
HAVING
Sum(invoices.Amount) <> 0
ORDER BY
customers.customerName
The query returns a list of users who have a non-zero balance on their invoice. The results from environment A resemble the following:
customerName countryName Balance
------------ ----------- -------
A United States 20.0000
B United States -5.0000
C Canada 199.9900
D Canada -0.0100
E United States 55.5900
The results on environment B are as follows:
customerName countryName Balance
------------ ----------- -------
A United States 10.0000
A United States -5.0000
A United States -1.0000
A United States 17.0000
A United States -1.0000
B United States -1.0000
B United States -4.0000
C Canada 100.9900
C Canada 99.9900
...
Query works fine on Environment A, but it looks as if the GROUP BY clause is being ignored altogether in Environment B.
The query works as expected when I comment out Sum(invoices.Amount)
from the SELECT and HAVING clause, so its definitely connected to my invoices.Amount
field.
I've backed up A and restored it to B, and SqlDelta (a diff tool for databases) shows that databases are identical, so its not a data issue, it has to be a configuration problem -- but I have no idea where to look.
In case its relevant, both environments are using SQL Server 2000 on a Windows Server 2003. Both environments are installed on two separate servers, separate instances of SQL Server, and separate instances of Windows Server 2003.
What could cause SQL server not to group the invoices.Amount
field?