I have two tables in which I my goal is to create a new column that calculates Calls per hour by simply dividing Calls by Hours. However, first I want to add the hours from table2 to table 1 before doing that calculation, adding the hours for the corresponding Name and Date.
Table1:
Name Calls Hours Date
--------------------------------------
John 50 3 9/10/2012
Paul 25 2 9/10/2012
Cindy 60 3 9/10/2012
David 50 2.5 9/10/2012
John 45 4 9/9/2012
Paul 50 5 9/9/2012
Cindy 20 2 9/9/2012
David 20 2 9/9/2012
Table 2:
Date Name Hours
------------------------------
9/10/2012 John 1
9/10/2012 Paul 2
9/10/2012 Cindy 2
9/9/2012 John 2.5
9/9/2012 Paul 1.5
9/9/2012 Cindy 0.5
Ultimately my desired query output is this:
Name Calls TotalHours CallsPerHour Date
-----------------------------------------------------
John 50 4 12.5 9/10/2012
Paul 25 4 6.25 9/10/2012
Cindy 60 5 12 9/10/2012
David 50 2.5 20 9/10/2012
John 45 6.5 6.9230 9/9/2012
Paul 50 6.5 7.6923 9/9/2012
Cindy 20 2.5 8 9/9/2012
David 20 2 10 9/9/2012
P.S. Names are unique in db.
Since both tables can have only one row per
NameperDate, you don’t need aGROUP BYquery. UseLEFT JOINto accommodate missing Name/Date combinations inTableTwo.Using your revised sample data, this is the result set from that query.