Solved

Adding another table

Posted on 2008-06-10
3
216 Views
Last Modified: 2010-04-21
Morning all,

I several tables:

wbCostCentre = List of cost centres
wbCompany = Company details
wbAssetTrack = ties an asset (i.e. mobile) to an employee
wbAssetTrack = copy of the wbAssetTrack with start and end dates of ownership
wbMCD = Mobile information
wbMessage = Call data (in this case SMS messages)
wbCostManager = List of cost centre managers


The join as I have it now works fine, it lists each cost centre and the charges incurred in a given time period (INVNUMBER), however I now need to also introduce a history table (wbAssetHist) which is a duplicate of the original wbAssetTrack data.

So lets say that a mobile belonged to CostID 131 from 1st May 2008 - 20th May 2008, the mobile was then assigned to CostID 132 from the 21st May to current, the total invoice value was £100 and the charges between the 1st May - 20th May where £40 and the charges between 21st May to the end of invoice was £60.

Currently what happens is that the CostID 131 shows the full charges of £100 which I expect because the query uses the MobdevID, if I add D.CostID = A.CostID to the wbMessage join after INVNUMBER then it will show the £40 correctly, what I can't figure out is how to include the history table into the join, so it collects data not only based on the current AssetTrack table but also adds any history charges to the correct CostID.


The rsults I get from this example are

131      178.892      Default Cost Centre      12345
132      0.000      sale                            67890

what I would expect to see is

131      178.892      Default Cost Centre      12345
132      38.789      sale                             67890

I'm not getting the charges for the CostID 132




Hopefully this makes some sense

Many thanks


SELECT
A.CostID,
CONVERT(DECIMAL(10,3), SUM(ISNULL(D.CHARGE,0))) AS CHARGE,
COSTNAME,
COSTNUMBER
 
FROM wbCostCentre A LEFT JOIN wbCompany B ON A.CompID = B.CompID
                                             LEFT JOIN wbAssetTrack  E ON E.CostID = A.CostID
                                                   LEFT JOIN wbAssetHist  G ON G.CostID = A.CostID
                                                          LEFT JOIN wbMCD C ON C.AssID = E.AssID
                                                                 LEFT JOIN wbvoiceout D ON D.MobDevID = C.MobDevID AND INVNUMBER = '92007' AND D.COSTID = A.COSTID
                                                                        LEFT JOIN wbCostManager F ON F.CostID = A.CostID
 
 
WHERE A.COMPID= 16 AND  F.EMPID = 2102 OR A.COMPID= 16 AND  INVNUMBER IS NULL  AND F.EMPID = 2102
GROUP BY A.CostID,COSTNAME,COSTNUMBER
ORDER BY A.CostID

Open in new window

0
Comment
Question by:trojan_uk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

Accepted Solution

by:
Mark Wills earned 250 total points
ID: 21756879
OK, you do need to get the history Cost ID into the group by as well. Otherwise there will never be anything other than the current CostID,

What I am unsure about is the £178 would appear to be total charges, and incorporates the historical charges as well. Introducing the historical costid would break that total... So in some circumstances, it almost is a self defeating query - either get the current total, or get the break down. We can do that through a union, but need a bit more information from you...

so as a starting point, lets just check what happens with  :


SELECT
A.CostID as Current_CostID,
G.CostID as Historic_CostID,
CONVERT(DECIMAL(10,3), SUM(ISNULL(D.CHARGE,0))) AS TOTAL_CHARGE,
CONVERT(DECIMAL(10,3), SUM(case when isnull(g.costid,0) = 0 then 0.00 else ISNULL(D.CHARGE,0) end) ) AS HISTORIC_CHARGE,
COSTNAME,
COSTNUMBER
 
FROM wbCostCentre A 
LEFT JOIN wbCompany B ON A.CompID = B.CompID
LEFT JOIN wbAssetTrack  E ON E.CostID = A.CostID
LEFT JOIN wbAssetHist  G ON G.CostID = A.CostID
LEFT JOIN wbMCD C ON C.AssID = E.AssID
LEFT JOIN wbvoiceout D ON D.MobDevID = C.MobDevID AND INVNUMBER = '92007' AND D.COSTID = A.COSTID
LEFT JOIN wbCostManager F ON F.CostID = A.CostID
 
WHERE A.COMPID= 16 AND  F.EMPID = 2102 OR A.COMPID= 16 AND  INVNUMBER IS NULL  AND F.EMPID = 2102
GROUP BY A.CostID,G.CostID,COSTNAME,COSTNUMBER
ORDER BY A.CostID,G.CostID

Open in new window

0
 
LVL 1

Assisted Solution

by:hasander
hasander earned 250 total points
ID: 21757514
You should not specify f.EmpID in the where clause since you have specified left join on wbCostManager. Move the where clause into the join condition instead, like the example below:
SELECT
A.CostID as Current_CostID,
G.CostID as Historic_CostID,
CONVERT(DECIMAL(10,3), SUM(ISNULL(D.CHARGE,0))) AS TOTAL_CHARGE,
CONVERT(DECIMAL(10,3), SUM(case when isnull(g.costid,0) = 0 then 0.00 else ISNULL(D.CHARGE,0) end) ) AS HISTORIC_CHARGE,
COSTNAME,
COSTNUMBER
 
FROM wbCostCentre A 
LEFT JOIN wbCompany B ON A.CompID = B.CompID
LEFT JOIN wbAssetTrack  E ON E.CostID = A.CostID
LEFT JOIN wbAssetHist  G ON G.CostID = A.CostID
LEFT JOIN wbMCD C ON C.AssID = E.AssID
LEFT JOIN wbvoiceout D ON D.MobDevID = C.MobDevID AND INVNUMBER = '92007' AND D.COSTID = A.COSTID
LEFT JOIN wbCostManager F ON F.CostID = A.CostID AND  F.EMPID = 2102 
 
WHERE A.COMPID= 16 OR A.COMPID= 16 AND  INVNUMBER IS NULL GROUP BY A.CostID,G.CostID,COSTNAME,COSTNUMBER
ORDER BY A.CostID,G.CostID

Open in new window

0
 

Author Closing Comment

by:trojan_uk
ID: 31465668
Sorry for the delay I have been away, I worked it out using a union join, but both your suggestions pointed me in the right direction.

Many thanks
Peter
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question