AussieNoobie


Hi all,

I am a newcomer to Microsoft Database Technology and have appeared to come across an issue with the SUM function in SQL Server Mobile Edition.

I am running Visual Studio 2005 and have created 2 tables:

Orders and OrderLines which are set up in master detail fashion.

The SQL Statement I create in the Query Builder is as follows:

SELECT     Orders.OrderNo, Orders.OrderDate, Orders.Priority, Orders.Address, SUM(OrderLines.Quantity * OrderLines.QualityReference) AS Total
FROM         Orders, OrderLines
WHERE     Orders.OrderNo = OrderLines.OrderNo
GROUP BY Orders.OrderNo, Orders.OrderDate, Orders.Priority, Orders.Address

Now, the SUM returns a total for all records in the OrderLines table, and not just the records whose OrderNo is the same as Orders.OrderNo

Can someone out there please clarify whether its an issue with my code or a bug with SQL Server Mobile

Here are a couple of screenshots which will demonstrate what I mean.

Here is the contents of the Orders and OrderLines tables.  Each order has only 1 line item in it.

 http://public.fotki.com/AussieNoobie/sqlerrors/vs2005sqlceordersdata.html

When performing the summation over the OrderLines table, it produces the SUM of the all records in OrderLines and not according to the GROUP BY clause. See the following screen shot.

http://public.fotki.com/AussieNoobie/sqlerrors/vs2005sqlceerror1.html

I hope this explains it better.

Anyone have any ideas  

Thanks in advance

 




Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

AussieNoobie


... I edited the above post and included screenshots of the issue I am experiencing.

Thanks again.






Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

AussieNoobie

Has anyone tried to reproduce this problem If it is a bug, it should be pretty easy to reproduce.

Any help would be appreciated.

Thanks Again.






Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

Pragya MSFT

This might be a bug in our code.

I have trouble reproducing the problem. Can you share the database (or just the schema) so that I can take a look.

Thanks

Pragya






Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

AussieNoobie

 I cant seem to attached files to this forum ...

Whats your email address and I'll email it to you

Otherwise, do you have a location I can upload it to

Thanks again.





Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

Pragya MSFT

Please mail to pragya dot agarwal at microsoft dot com

Thanks

Pragya






Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

AussieNoobie

Hi Pragya,

The Orders Table schema is available here:

http://public.fotki.com/AussieNoobie/sqlerrors/orders.html

The OrderLines Table schema is available here:

http://public.fotki.com/AussieNoobie/sqlerrors/orderlines.html

The relationship setup in the VS2005 project is as follows:

http://public.fotki.com/AussieNoobie/sqlerrors/ordersorderlinesrel.html

The Database XSD is available here:

http://public.fotki.com/AussieNoobie/sqlerrors/dbxsd.html

Thanks again for your help.





Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

AussieNoobie

Hi Pragya,

I have emailed you the SDF file in question.

Thanks again.





Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

ErikEJ

Using Northwind, the following query works fine:

SELECT Orders.[Ship Name], SUM([Order Details].[Unit Price]) AS Total
FROM [Order Details] INNER JOIN
Orders ON [Order Details].[Order ID] = Orders.[Order ID]
GROUP BY Orders.[Ship Name]

So select only from the Order Details, and inner join on Orders.

Hope this assists.






Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

AussieNoobie

I have just been doing some testing and found the following:

If I issue the following statement:

SELECT Orders.OrderID, Orders.OrderNo, Orders.OrderDate, Orders.Priority, SUM(OrderLines.Quantity * OrderLines.QualityReference) AS Total
FROM Orders, OrderLines
WHERE Orders.OrderID = OrderLines.OrderID
GROUP BY Orders.OrderNo, Orders.OrderDate, Orders.Priority, Orders.OrderID

I get the incorrect values:

OrderID, OrderNo, OrderDate, Priority, Total

1 53518890 9/01/2007 10:34:22 AM Normal 146.0800
2 53518891 11/01/2007 9:33:00 AM Normal 146.0800

However, if I select the OrderLines.OrderID instead of the Orders.OrderID, it returns the correct values:

SELECT OrderLines.OrderID, Orders.OrderNo, Orders.OrderDate, Orders.Priority, SUM(OrderLines.Quantity * OrderLines.QualityReference) AS Total
FROM Orders, OrderLines
WHERE Orders.OrderID = OrderLines.OrderID
GROUP BY Orders.OrderNo, Orders.OrderDate, Orders.Priority, OrderLines.OrderID

OrderID, OrderNo, OrderDate, Priority, Total

1 53518890 9/01/2007 10:34:22 AM Normal 108.0900
2 53518891 11/01/2007 9:33:00 AM Normal 37.9900





Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

Nick Randolph

This appears to be an issue with having arguments in the group by clause that do not appear in the join - for example if you run the following you get the right values.

SELECT Orders.OrderNo, SUM(OrderLines.Quantity * OrderLines.QualityReference) AS Total
FROM Orders inner join OrderLines
on Orders.OrderNo = OrderLines.OrderNo
GROUP BY Orders.OrderNo

However, if you run the following you get the wrong values:

SELECT Orders.OrderDate, SUM(OrderLines.Quantity * OrderLines.QualityReference) AS Total
FROM Orders inner join OrderLines
on Orders.OrderNo = OrderLines.OrderNo
GROUP BY Orders.OrderDate

If you include an OrderDate field in the OrderLines table (agreed this is not what you want to do but hear me out) and make sure the values correspond to the OrderDate in the Orders table. Then when you run the following query you again get the right values:

SELECT Orders.OrderDate, SUM(OrderLines.Quantity * OrderLines.QualityReference) AS Total
FROM Orders inner join OrderLines
on Orders.OrderDate= OrderLines.OrderDate
GROUP BY Orders.OrderDate

As you stated if you do the group by based on values in the OrderLines table it always seems to return the correct values. I must admit from my testing there seems to be a fundamental issue here - I'm sure there is a logical explanation but it would be great for the product team to follow up on what we are doing wrong!






Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

AussieNoobie

This is fixed by creating an Index on the foreign key column of the OrderLines table.

ie. Create an index on OrderLines.OrderID and this issue will go away ...

Now the queries return the correct values and all is good





Re: SUM returns incorrect value in SQL Server Mobile Query ... Bug??

Pragya MSFT

This is a bug and we are working on it.

For those who are running into this issue, try the following workarounds:

1) Create index on both join columns OR

2) Remove indexes from both join columns.

Thanks

Pragya