Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding another table

Posted on 2008-06-10
3
Medium Priority
?
218 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 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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