Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sajitha
Starting Member
10 Posts |
Posted - 2014-03-26 : 06:04:15
|
From the below given table, how can I find out TotalCost ie, Sum ( TableB.WCost * [ Table B. Price + ( Table C. Time * Table D. Cost ) ] ) if the condition is:- Date is between 10/04/2014 AND 11/04/2014 AND Category = categoryA AND Eid =2 AND Pid =2 AND Fid =1 The expected TotalCost is 1379.902 ((2.236 + 14.716 + 576.95 + 786) = 1379.902 ) [ 23.6 + ( 2 * 100 ) ] * 0.01 = 2.236[ 67.9 + ( 1.5 * 200 ) ] * 0.04 = 14.716[ 100.78 + ( 1.30 * 100 ) ] * 2.5 = 576.95[ 445 + ( 1.5 * 140 ) ] * 1.2 = 786 Table A Table B Table C Table DAID BID CID DIDDate AID AID CostCategory Price Time Eid WCost DIDFidPidTable AAID Date Category Eid Fid Pid 1 10/04/2014 categoryA 2 1 2 2 10/04/2014 categoryB 2 1 23 10/04/2014 categoryC 2 1 24 24/04/2014 categoryA 3 2 1Table BBID AID Price WCost 1 1 23.6 0.012 1 67.9 0.04 3 1 100.78 2.54 2 445 1.2Table CCID AID Time DID1 1 2 12 1 1.5 2 3 1 1.30 1 4 2 1.5 3 Table DDID Cost1 1002 2003 1404 123Thank you in advance |
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-03-26 : 07:39:29
|
Without having the data I cant test but something along the lines of this...to add additional fields you would need to include fields after the select separated by comms...this would only return the TotalCost.Select Sum(b.WCost * b.Price + c.Time * d.Cost) as TotalCostFrom Tableb bJoin Tablec c on b.BID = c.CIDJoin Tabled d on b.BID = d.DIDJoin Tablea a on b.BID = a.AIDWhere a.Date Between '2014-04-10' And '2014/04/11'And a.Category = 'categoryA'And a.Eid = 2And a.Pid = 2And a.Fid = 1We are the creators of our own reality! |
 |
|
|
|
|