Solved

Adding another table

Posted on 2008-06-10
3
217 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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