Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

aggregate function and join

$
0
0

I have three Tables(Emptimesheet,comexpensetable,perexpensestable).I want to show otput something like below,

EmpID,EmpName,StartDate,EndDate,Total,Chotel,CAirfare,Cfuel,WeeklCompanyExpenses,Percompanyexpenses

rightnow in the below query I don't have hotel,airfare,fuel.

select distinct(e.EmpID) ,e.EmpName, e.StartDate,e.EndDate,e.Total,c.WeeklyComtotalExpenses,p.WeeklyPerTotalExpenses
 from EmpTimeSheet e  left JOIN  ComExpensesTable c   on c.CExpID=e.EmpID   left join  PerExpensesTable p ON p.PExpID=e.EmpID

Output for Above query .

EmpID name sd ED Total companEx PerExpense

1 tom 2014-02-02 00:00:00.000 2014-02-08 00:00:00.000 28:30:0 213.00 199.00 3 nick 2014-03-02 00:00:00.000 2014-03-08 00:00:00.000 15:20:0 16.00 NULL 4 john 2014-03-23 00:00:00.000 2014-03-29 00:00:00.000 42:0:0 NULL NULL 5 test 2014-03-09 00:00:00.000 2014-03-15 00:00:00.000 34:15:0 NULL NULL 6 ema 2014-04-13 00:00:00.000 2014-04-19 00:00:00.000 39:0:0 16.00 NULL


Output  Emptimesheet

id empname startdate enddate .. total

1 tom 2014-02-02 00:00:00.000 2014-02-08 00:00:00.000 Thurs 2014-02-06 00:00:00.000 Travel Time 28:30:0 3 nick 2014-03-02 00:00:00.000 2014-03-08 00:00:00.000 Mon 2014-03-03 00:00:00.000 Onsite/WalkTesting 40:0:0 4 john 2014-03-23 00:00:00.000 2014-03-29 00:00:00.000 Mon 2014-03-24 00:00:00.000 Onsite/WalkTesting 42:0:0 5 test 2014-03-09 00:00:00.000 2014-03-15 00:00:00.000 Mon 2014-03-10 00:00:00.000 Travel Time , 34:15:0 6 ema 2014-04-13 00:00:00.000 2014-04-19 00:00:00.000 Mon 2014-04-14 00:00:00.000 Equipment Testing 39:0:0

output for ComExpenseTable 

CExpID airfare hotel weeklycomexpensetable

1 45.00 34.00 213.00 3 5.00 3.00 16.00 6 5.00 3.00 16.00


output  for  Perexpensetable

PExpID PerExpenseTable

1 Sun 2014-02-02 00:00:00.000 199.00 2 Sun 2014-02-23 00:00:00.000 199.00

below  query for sum of indivial expense in company table

select sum(CHotel) as Hotel ,sum(CTransport) as Airfare,sum(CMeals) as Meals,sum(CFuel) as Gas,sum(CTolls),Sum(CParking) as Parking,Sum(CMisc) as Misc ,sum(CMileage) as Mileage from  ComExpensesTable  where CExpID=1
Now  I want  to add  sum of hotel,transport ..  to  the below query  .I don't  know how  to do this.Any suggestions please?
select distinct(e.EmpID) ,e.EmpName, e.StartDate,e.EndDate,e.Total,c.WeeklyComtotalExpenses,p.WeeklyPerTotalExpenses
 from EmpTimeSheet e  left JOIN  ComExpensesTable c   on c.CExpID=e.EmpID   left join  PerExpensesTable p ON p.PExpID=e.EmpID


 



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>