views:

186

answers:

3

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?

+3  A: 

Try this query to attempt to reproduce the problem in a simpler form:

SELECT customerName, Count(*)
FROM customer
GROUP BY customerName
ORDER BY customerName

Try this query to check out the possibility of trailing spaces in the customer name as a reason to group differently.

SELECT customerName, Count(*)
FROM
(
SELECT '|' + customerName + '|' as customerName
FROM customer
) as sub
GROUP BY customerName
ORDER BY customerName
David B
+1  A: 

What happens if you comment out the HAVING clause, but leave Sum(invoices.Amount) in the SELECT clause? i.e. can you verify that the sum() function is working?

Also, you might check your database collation (or your default server collation); collation settings can affect some aggregate functions, but admittedly, I don't see why it would alter the results of sum().

Matt
IT WORKS! So, its definitely the HAVING clause causing the problem, otherwise the default collation between the two servers is the same. I should have noticed this before, but it looks like Environment A has SQL Server 8.0.2039, where environment B has SQL Server 8.0.1053. Looks like I haven't installed some service packs -- I'll give that a shot and see what happens.
Juliet
Ha! Good catch on the version numbers; 8.00.2039 is SP 4, so at least it'll be easy to apply in your second environment.
Matt
+2  A: 

It turns out there was no error with the query, it was an environment problem. Environment A had SQL Server 8.0.2039, where environment B has SQL Server 8.0.1053. I've updated environment B with the latest service packs and now my query works as expected.

Thanks for your suggestions everyone :), but now I have a new problem: who to kill at Microsoft for this hassle.

Juliet
why kill them? sql 2000 is a piece of junk, you should get resources for 2005 or 2008!
DForck42
Thirster: would love to! While everything we develop in-house is done in 2005, we depend on some 3rd party vendors who are tied to 2000. Not a whole lot I can do there :)
Juliet