trojan_uk
asked on
Adding another table
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks
Peter