473,383 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

SUM does not return expected results when used with JOIN

I have two tables that I am trying to use to create a report by using
a join. When the query is executed, the SUM on the second table is 4
times the expected amount. As a stand alone query on the second table,
this works:
SELECT `Last_Name`, `First_Name`, `Employee_SSN`,
SUM(`id_es_inv`.`Total_Amount`) AS `Expense Amount`
FROM `id_es_inv`
GROUP BY `Last_Name`

But when I attempt the same operation using a JOIN on the first table,
the SUM then comes out as 4x the expected value:
SELECT
SUM(`id_es_inv`.`Total_Amount`) AS `Expense Amount`,
CONCAT_WS(', ',id_ts_inv.`Last_Name`,id_ts_inv.`First_Name`) AS
`Name`, SUM(id_ts_inv.Total_Amount) As `Total`,
SUM(id_ts_inv.`Hours_Reg`) AS `REG Hrs`, SUM(id_ts_inv.`Hours_OT`) AS
`OT Hrs`, SUM(id_ts_inv.`Hours_DT`) AS `DT Hrs`,
id_ts_inv.`Rate_Regular_Bill` AS `Bill Rate`,
SUM(id_ts_inv.`Hours_Reg`* id_ts_inv.`Rate_Regular_Bill`) AS `Amount
ST`,
SUM(id_ts_inv.`Hours_OT`* id_ts_inv.`Rate_Regular_Bill`) AS `Amount
OT`,
SUM(id_ts_inv.`Hours_DT`* id_ts_inv.`Rate_Regular_Bill`) AS `Amount
DT`
FROM `id_ts_inv`
`id_ts_inv` LEFT JOIN `id_es_inv` USING (`Employee_SSN`)
GROUP By id_ts_inv.`Last_Name`, `id_es_inv`.`Last_Name`
The tables are not identicle in structure, i.e `id_es_inv` contains a
smaller number of columns and rows than `id_ts_inv`, so I can't use a
UNION with 2 selects. I've also tried changing the JOIN columns, but
that just made things worse.

I've poured over the Internet, gone through everything relevant in
Paul DuBois's MySQL CookBook, asked friends who have more MySQL
experience that I do, but no luck yet.

Can anybody help with a useful suggestion as to why this behavior is
happening?
TIA
Jul 19 '05 #1
2 2028
"Richard J Lacroix" <ri**********@comcast.net> wrote in message
news:20**************************@posting.google.c om...
[snip]
What is the purpose of repeating `id_ts_inv` in the FROM?
FROM `id_ts_inv`
`id_ts_inv` LEFT JOIN `id_es_inv` USING (`Employee_SSN`)


--
Eric Lafontaine
Jul 19 '05 #2
That's just a cut-and-paste error.

Richard

"ventre-à-pattes" <ne**@ventre-a-pattes.com> wrote in message news:<be*********@imsp212.netvigator.com>...
"Richard J Lacroix" <ri**********@comcast.net> wrote in message
news:20**************************@posting.google.c om...
[snip]
What is the purpose of repeating `id_ts_inv` in the FROM?
FROM `id_ts_inv`
`id_ts_inv` LEFT JOIN `id_es_inv` USING (`Employee_SSN`)

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hunter Hillegas | last post by:
I have just added a third table to a query and I am no longer getting the results I am expecting. Three Tables: CUSTINVOICEJOUR (Header Table) CUSTINVOICETRANS (Line Item Table) MARKUPTRANS...
2
by: smauldin | last post by:
Why does the execution plan have a nested loop join for a simple select with an UDF in the where clause? Here is the query: select * from test_plan where vCol = my_udf('test') Here is the...
2
by: Robert | last post by:
I built a query in Query Analyzer and mapped it to Access 2002. The result set returned is identical except cells in some records in the Access result have #Deleted. This cast a shadow of doubt...
1
by: Ken | last post by:
I wrote a function to use in queries that takes a date and adds or subtracts a certain length time and then returns the new value. There are times when my function needs to return Null values. ...
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
45
by: salad | last post by:
I'm curious about your opinion on setting relationships. When I designed my first app in Access I'd go to Tools/Relationships and set the relationships. Over time I'd go into the window and see...
25
by: Peter Michaux | last post by:
Hi, I'm thinking about code minimization. I can think of a few places where whitespace matters a + ++b a++ + b a - --b a-- -b when a line ends without a semi-colon in which case the new...
2
by: KTosser | last post by:
I have two tables, one contains contacts and the other has all the events and years that the contacts participated in. What I want is to be able to choose the years, say 2005 and 2006, and get all...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.