How could SUM(column) give incorrect results in MySQL? - mysql

I have the following simple MySQL query, called from PHP:
SELECT foo_id, SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id
GROUP BY foo_id
This works fine, except for one $foo_id, which returns about 2.5 times greater than the sum of the number_of_guests field.
What could cause this behavior for only a certain value of $foo_id?
Is there a better way to do this?

Your query should work. The error is most likely in the other method you are using to verify the result.
Is there a better way to do this?
Yes. Since you are only fetching one group there's no need for your GROUP BY clause:
SELECT SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id

The problem is that there was one row with a large value for number_of_guests. I didn't see in in browsing the data because there are a few hundred rows. It didn't show up when I copied and pasted from HTML page into Excel because that row was missing most of the other columns, and the HTML page has all the columns.
Thanks for all your help!

Related

MS Access Query Goes Blank If No Rows In Another Query

This has been driving me nuts for awhile now.
So I have Query_A and Query_B that I need to combine into one query called Query_C.
This works perfectly fine. But if Query_B returns no results or zero rows because of a filter, then Query_C goes blank or to zero rows as well.
Is there any way to have Query_C still display the results of Query_A even if Query_B has no rows?
I have been doing null to zero in the past which works fine but its a pain to do it for each column and alot of times I have to make another query just for doing the null to zero part. And on top of it then I have formatting problems and have to format each column as well. It always ends up being so many queries and so much extra work it just seems dumb.
Is there a better way of doing this?
Consider a subquery UNION or UNION ALL
SELECT *
FROM
(Query_A UNION Query_B) AS Query_C
Adding a left join solved the problem. Made a dummy feild in each table and joined them with that for the test.

Grouping/Summing query in Access DB

I have a table with multiple fields that I am grabbing information from (8 to be specific) but within this query I need to group on one field (ID's) and sum two other fields separately (Units, MV). I can pull all the information together in 2-3 queries but I want to know if i can pull/put everything together in one query.
The correct result would be a query that groups the results on the ID field only, has both Units and MV summed separately but also have the other fields I need too. I can get part of the results in one query if I only have the grouped ID field and summed Units and MV fields but if I add any other fields then the results are incorrect. Any assistance would be appreciated.
I ended up messing around with using DISTINCT within the SQL portion of access and finally got the results. Thank you for all your help!

Data filtering on form generates "Could not find field"

I spent half day trying to figure out why appears an error messagebox
Could not find field 'TransactionTypeID'
in my
database. If you open Form1, then apply any filter on column TransactionTypeID using header (for instance, uncheck Blanks) and then try to open sorting/filtering for second column, appears error message.
Error disappears if I convert combobox to text box or remove from form select table Tenants1. I use Access 2010 32 bit. In this example I simplified tables as much as possible, database created from scratch, data imported, compact/repair doesn't help.
Do you have any ideas?
I found the problem. The built-in datasheet form filtering works in a wrong way if tables joined this way:
SELECT VouchersMain1.VDate, VouchersMain1.TransactionTypeID
FROM Tenant1 INNER JOIN VouchersMain1 ON Tenant1.TenantID = VouchersMain1.TenantID;
If I reverse tables join direction, built-in filtering works fine:
SELECT VouchersMain1.VDate, VouchersMain1.TransactionTypeID
FROM VouchersMain1 INNER JOIN Tenant1 ON VouchersMain1.TenantID = Tenant1.TenantID;
Looks like this is another Access bug.
Also, thanks #Munsterlander, problem disappears if form's recordsource replaced by saved query instead of SELECT
Try referencing your field as Forms!FORMNAME!CONTROLNAME. I assume, based off what you wrote, you are trying to filter a query based on what is selected in the combobox.
Delete the table Tenants1 from your form RecordSource (this table is not necessary and do not exposes fields in the resulting query).
You would also note that your recordsource is set (by Access) ReadOnly (bad design, no join defined). Try to add a couple of Records in the Tenant1 table, say it David and Nathan.
You will find that now your query will output 6 records (and not 2), because the query (with no joins) list one row for all records of table Tenant1 (3) and one row for each record of table VouchersMain1 (2), giving a total of 2*3=6 rows.

Sort a View SQL Server 2014

I am trying to find a way to sort the results in a view. I know the Order by command does not work. The reason for this is that we are using a BI tool 'Board' to drill through to the raw data. Unfortunately there is no way in the program to sort a drill through as I would a regular query since the Where clause is dynamic and the program always puts it at the end of the query.
Hence an Order by would appear prior to a Where which results in an error.
Any suggestions would be greatly appreciated.
Thanks,
Scott
So there's a couple of ways to achieve this, one is to make sure that the data the view is reading is already sorted, to do this, you make sure that the indexes on the table/s that the view is reading from are sorted the way you want, if this isn't possible, then you could also create a clustered index on the view itself and specify the sort order there.
If creating indexes is not possible, there is another way that I have found to work, but is not always ideal as it creates an extra column in your result set that doesn't mean anything, you can do this:
SELECT ROW_NUMBER() OVER ( ORDER BY [col1] ASC ) AS [sort_col] ,
[col1] ,
[col2] ,
[col3]
FROM [dbo].[table];
If you do this, you will end up with the extra column giving the row number, but the data will be sorted by [col1]. One thing to mention with this is that if you do put this into the view definition, you must still select that column in your query for the sorting to take place.

Mysql sum as last row

Is it possible to have the SUM of all numaric fields in the last of a set of rows?
As of now, I'm using a very simple query such as:
SELECT
*,
SUM((UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start))/3600)
FROM
times
in SQL you cant have a column that appears in only one row, likewise, you also cant have a row that doenst contain all the columns from the other rows.. So having a row that contains something unique is not possible. You can, however, add the calculated column to all rows in the dataset or do the calculation in the calling code after the data is returned.
I think what you are looking for is GROUP BY WITH ROLLUP you will find details on that in the MySQL manual.