473,395 Members | 1,583 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,395 software developers and data experts.

Query not showing cost correctly

374 256MB
All,

I have a query which picks out the final date of the last email sent within my system. The query is as follows:

Expand|Select|Wrap|Line Numbers
  1. 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
  2. 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
  3. 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?


Nov 27 '10 #1

✓ answered by Stephen Parker

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:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. tbllog.NCC_ID, 
  3. tbllog.DteReport, 
  4. tbllog.DteOccur, 
  5. tbldept_1.Department AS DeptRaisedBy, 
  6. tbldept.Department AS DeptResp, 
  7. tblstatus.NCStatus, 
  8. A.SumOfCostFig, 
  9. Last(tblEmail.EmailDate) AS LastOfEmailDate 
  10. FROM ((tblstatus 
  11. INNER JOIN 
  12. ((tbllog 
  13. INNER JOIN tbldept AS tbldept_1 
  14. ON tbllog.DeptRaisedBy = tbldept_1.DeptID) 
  15. INNER JOIN tbldept 
  16. ON tbllog.DeptResp = tbldept.DeptID) 
  17. ON tblstatus.NCStatusID = tbllog.NCStatus) 
  18. LEFT JOIN (
  19. SELECT NCC_ID, Sum(CostFig) AS SumOfCostFig
  20. FROM tblCosts 
  21. GROUP BY NCC_ID) AS A
  22. ON tbllog.NCC_ID = A.NCC_ID) 
  23. LEFT JOIN tblEmail ON tbllog.NCC_ID = tblEmail.NCC_ID1 
  24. GROUP BY 
  25. tbllog.NCC_ID, 
  26. tbllog.DteReport, 
  27. tbllog.DteOccur, 
  28. tbldept_1.Department, 
  29. tbldept.Department, 
  30. tblstatus.NCStatus,
  31. A.SumOfCostFig; 
  32.  

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:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. tbllog.NCC_ID, 
  3. tbllog.DteReport, 
  4. tbllog.DteOccur, 
  5. tbldept_1.Department AS DeptRaisedBy, 
  6. tbldept.Department AS DeptResp, 
  7. tblstatus.NCStatus, 
  8. A.SumOfCostFig, 
  9. Last(tblEmail.EmailDate) AS LastOfEmailDate 
  10. FROM ((tblstatus 
  11. INNER JOIN 
  12. ((tbllog 
  13. INNER JOIN tbldept AS tbldept_1 
  14. ON tbllog.DeptRaisedBy = tbldept_1.DeptID) 
  15. INNER JOIN tbldept 
  16. ON tbllog.DeptResp = tbldept.DeptID) 
  17. ON tblstatus.NCStatusID = tbllog.NCStatus) 
  18. LEFT JOIN (
  19. SELECT NCC_ID, Sum(CostFig) AS SumOfCostFig
  20. FROM tblCosts 
  21. GROUP BY NCC_ID) AS A
  22. ON tbllog.NCC_ID = A.NCC_ID) 
  23. LEFT JOIN tblEmail ON tbllog.NCC_ID = tblEmail.NCC_ID1 
  24. GROUP BY 
  25. tbllog.NCC_ID, 
  26. tbllog.DteReport, 
  27. tbllog.DteOccur, 
  28. tbldept_1.Department, 
  29. tbldept.Department, 
  30. tblstatus.NCStatus,
  31. A.SumOfCostFig; 
  32.  
Nov 28 '10 #2
munkee
374 256MB
Works perfectly Stephen thanks!
Nov 30 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
3
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...
3
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....
1
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
1
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...
2
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...
6
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
4
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...
5
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 -...
2
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

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.