Solved

Adding another table

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now