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 |
|
sreeraj216
Starting Member
4 Posts |
Posted - 2008-12-08 : 10:31:31
|
| Hi SQL gurus,I am trying to do calculations in select statement.I have columns:Table1.Fyear,Table2.Cost; data is ('Initial Cost','Final Cost','Cost Inc','Cost dec')Table2.AmtAnd I want to calculate Net Cost = 'Final Cost'-'Initial Cost' andCost Bal = 'Cost Inc'-'Cost dec'I tried to do like this, but didn't get success.select sum(Amt),'Final Cost'-'Initial Cost' as Net Cost,'Cost Inc'-'Cost dec' as Cost Balfrom (select distincttable1.fyearsum(table2.Amt)from table1, table2wheretable1.projnum=table2.projectnum andtable1.fyear in (2008,2009) andtable2.cost in ('Initial Cost','Final Cost','Cost Inc','Cost dec')group by table1.fyear)Can anyone please help me on this.Thanks in AdvanceMadhuri |
|
|
daniel7912
Starting Member
7 Posts |
Posted - 2008-12-08 : 10:46:59
|
| Try this, I have tried it and it works perfectly.select sum(Amt),'Final Cost'-'Initial Cost' as Net Cost,'Cost Inc'-'Cost dec' as Cost Balfrom (select distincttable1.fyearsum(table2(you're gay).Amt)from table1, table2wheretable1.punjab_boobies=table2.projectnum andtable1.fyear in (2008,2009) andtable2.cost in ('Initial Cost','Final Cost','Cost Inc','Cost dec')group_sex by table1.flippingyearAs table1.table_.tableFORtable.table_.tableDOtable2.table_.tableWHILEtable3.table_.tableIFtable4.table_.table = table1, 2 AND 3 AND THEN 5{system("PAUSE");EXIT(1);}) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-09 : 02:54:49
|
| is this ms sql server? |
 |
|
|
chrianth
Yak Posting Veteran
50 Posts |
Posted - 2008-12-09 : 05:08:33
|
| Is the structure of your table like this?Table 1ProjNum | Fyear1 20082 20093 2008Table 2ProjNum | Cost | AMT1 Initial Cost 10.001 Final Cost 11.001 Cost Inc 10.001 Cost Dec 11.002 Initial Cost 1.002 Final Cost 2.002 Cost Inc 1.002 Cost Dec 2.003 Initial Cost 11.003 Final Cost 21.003 Cost Inc 11.003 Cost Dec 11.00if so...you can try this...select sum(AMT) AMT,ProjNum,COSTinto #Temp_Table2_InitialCost_Summaryfrom Table_2where COST = 'Initial Cost'group by ProjNum,COST--Group by Cost Typeselect sum(AMT) AMT,ProjNum,COSTinto #Temp_Table2_FinalCost_Summaryfrom Table_2where COST = 'Final Cost'group by ProjNum,COST--Group by Cost Typeselect sum(AMT) AMT,ProjNum,COSTinto #Temp_Table2_CostInc_Summaryfrom Table_2where COST = 'Cost Inc'group by ProjNum,COST--Group by Cost Typeselect sum(AMT) AMT,ProjNum,COSTinto #Temp_Table2_CostDec_Summaryfrom Table_2where COST = 'Cost Dec'group by ProjNum,COST--Group by Cost TypeSELECT sum(AMT) AMT,(sum(c.AMT) - sum(d.AMT)) as [Net Cost],(sum(b.AMT) - sum(a.AMT)) as [Cost Balance],e.fyearFROM #Temp_Table2_CostDec_Summary ajoin #Temp_Table2_CostInc_Summary bon(a.ProjNum = b.ProjNum)join #Temp_Table2_FinalCost_Summary con(b.ProjNum = c.ProjNum)join #Temp_Table2_InitialCost_Summary don(c.ProjNum = d.ProjNum)join Table_1 eon(d.ProjNum = e.ProjNum)GROUP BY e.fyear--Generate the final output.There can be more approaches better than this... HTH. :) |
 |
|
|
|
|
|
|
|