All,
I have a query which picks out the final date of the last email sent within my system. The query is as follows: - SELECT tbllog.NCC_ID, tbllog.DteReport, tbllog.DteOccur, tbldept_1.Department AS DeptRaisedBy, tbldept.Department AS DeptResp, tblstatus.NCStatus, Sum(tblCosts.CostFig) AS SumOfCostFig, Last(tblEmail.EmailDate) AS LastOfEmailDate
-
FROM ((tblstatus INNER JOIN ((tbllog INNER JOIN tbldept AS tbldept_1 ON tbllog.DeptRaisedBy = tbldept_1.DeptID) INNER JOIN tbldept ON tbllog.DeptResp = tbldept.DeptID) ON tblstatus.NCStatusID = tbllog.NCStatus) LEFT JOIN tblCosts ON tbllog.NCC_ID = tblCosts.NCC_ID) LEFT JOIN tblEmail ON tbllog.NCC_ID = tblEmail.NCC_ID1
-
GROUP BY tbllog.NCC_ID, tbllog.DteReport, tbllog.DteOccur, tbldept_1.Department, tbldept.Department, tblstatus.NCStatus;
The basic schematic is:
nonconformance ID and info (from tbllog) + sum of costs (from cost table, can be multiple costs) + last email date sent associated with nonconformance ID (can be multiple emails sent)
The issue I have is that everytime I send a new email (becoming the last sent date) my sum of costs becomes multiplied by the number of email records associated with the same nonconformance ID.
For example:
NCC ID 160 with £500 total cost (2x £250 records) and 3 emails sent
My query outputs that the total cost is £1500.
How can I change my sql to stop this multiplication happening?
Assuming that the rest of your SQL works the way you want it to then it appears as though you simply need to isolate the sum statement. In other words maybe try to perform the sum then join it to the rest of the select statement to prevent the calculation from multiplying on each email. That is to say if it is neccessary that you have all of the multiple emails returned by the query and isolating a unique instance is no optional then maybe try something like this: -
SELECT
-
tbllog.NCC_ID,
-
tbllog.DteReport,
-
tbllog.DteOccur,
-
tbldept_1.Department AS DeptRaisedBy,
-
tbldept.Department AS DeptResp,
-
tblstatus.NCStatus,
-
A.SumOfCostFig,
-
Last(tblEmail.EmailDate) AS LastOfEmailDate
-
FROM ((tblstatus
-
INNER JOIN
-
((tbllog
-
INNER JOIN tbldept AS tbldept_1
-
ON tbllog.DeptRaisedBy = tbldept_1.DeptID)
-
INNER JOIN tbldept
-
ON tbllog.DeptResp = tbldept.DeptID)
-
ON tblstatus.NCStatusID = tbllog.NCStatus)
-
LEFT JOIN (
-
SELECT NCC_ID, Sum(CostFig) AS SumOfCostFig
-
FROM tblCosts
-
GROUP BY NCC_ID) AS A
-
ON tbllog.NCC_ID = A.NCC_ID)
-
LEFT JOIN tblEmail ON tbllog.NCC_ID = tblEmail.NCC_ID1
-
GROUP BY
-
tbllog.NCC_ID,
-
tbllog.DteReport,
-
tbllog.DteOccur,
-
tbldept_1.Department,
-
tbldept.Department,
-
tblstatus.NCStatus,
-
A.SumOfCostFig;
-
2 1415
Assuming that the rest of your SQL works the way you want it to then it appears as though you simply need to isolate the sum statement. In other words maybe try to perform the sum then join it to the rest of the select statement to prevent the calculation from multiplying on each email. That is to say if it is neccessary that you have all of the multiple emails returned by the query and isolating a unique instance is no optional then maybe try something like this: -
SELECT
-
tbllog.NCC_ID,
-
tbllog.DteReport,
-
tbllog.DteOccur,
-
tbldept_1.Department AS DeptRaisedBy,
-
tbldept.Department AS DeptResp,
-
tblstatus.NCStatus,
-
A.SumOfCostFig,
-
Last(tblEmail.EmailDate) AS LastOfEmailDate
-
FROM ((tblstatus
-
INNER JOIN
-
((tbllog
-
INNER JOIN tbldept AS tbldept_1
-
ON tbllog.DeptRaisedBy = tbldept_1.DeptID)
-
INNER JOIN tbldept
-
ON tbllog.DeptResp = tbldept.DeptID)
-
ON tblstatus.NCStatusID = tbllog.NCStatus)
-
LEFT JOIN (
-
SELECT NCC_ID, Sum(CostFig) AS SumOfCostFig
-
FROM tblCosts
-
GROUP BY NCC_ID) AS A
-
ON tbllog.NCC_ID = A.NCC_ID)
-
LEFT JOIN tblEmail ON tbllog.NCC_ID = tblEmail.NCC_ID1
-
GROUP BY
-
tbllog.NCC_ID,
-
tbllog.DteReport,
-
tbllog.DteOccur,
-
tbldept_1.Department,
-
tbldept.Department,
-
tblstatus.NCStatus,
-
A.SumOfCostFig;
-
Works perfectly Stephen thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Garry Clarke |
last post by:
I have enabled the query governor on our SQL2000 SP2 server with a
threshold of 3600. Now, some of the maintenance jobs fail due to the
limit being to low (e.g. one of the user databases integrity...
|
by: Ben Allen |
last post by:
Hi,
I'm using a piece of software called 'phpcoin' along with the popular
'IPN' mod. I am having some problem with it and have got it down to an
mySQL function functioning incorrectly.
This is...
|
by: xixi |
last post by:
hi,
we are using db2 udb v8.1 on win 64 bit with fp3 with type 4
db2jcc.jar driver.
i have a query, if using control center explain sql tool, i get the
access path graph with total cost 0.26....
|
by: Gary T. |
last post by:
I have a table called tbl_QuoteDetails
QuoteNo Prim Key
fkeyProductID foreign key
Paneltype
Length
Price
Another table called tbl_Product:
ProductID Primary Key
ProductName
|
by: traceable1 |
last post by:
I have 2 SQL databases which are the same and are giving me different
query plans.
select s.* from hlresults h
inner join specimens s on s.specimen_tk = h.specimen_tk
where s.site_tk = 9 and...
|
by: windsorben |
last post by:
I'm submitting a form to the same page. Form variables are being output into the URL and the query works from the URL variables. Problem is, I have submit the form twice before the form field I have...
|
by: xian2 |
last post by:
Hi All,
I have created a query with 6 related tables as the record source.
The query includes 4 fields from 4 of the tables:
tblTourBookings
TourStartDate
tblAdditionalCosts
|
by: sanonward001 |
last post by:
Hi,
This is regarding the query optimization.
T_A table is having rows around 570000, and this query's cost comes out to 202.
SELECT COUNT(1)
FROM T_A
WHERE...
|
by: Sandretto |
last post by:
Hey
I've made an outstanding order database for the company I work for. I've created a query which calculates how many are delivered on each job number and how many are outstanding. Using tables -...
|
by: drgoodvibe |
last post by:
Hi There,
I'm trying to write a query that returns the total sames amount by each sales person for each product -- I'm having issues with the join sequence at this moment. Any help would be much...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |