1

我有两个表,我的目标是创建一个新列,通过简单地将呼叫数除以小时数来计算每小时的呼叫数。但是,首先我想在进行计算之前将表 2 中的小时数添加到表 1 中,添加相应名称和日期的小时数。

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

最终我想要的查询输出是这样的:

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

PS 名称在 db 中是唯一的。

4

4 回答 4

3

这就是你所追求的吗?

select  t1.name, 
        t1.calls,
        t1.hours + t2.hours as TotalHours,
        t1.calls / (t1.hours + t2.hours) as CallsPerHour,
        t1.date
from    table1 t1 inner join table2 t2 on t1.name = t2.name and t1.date = t2.date
于 2012-09-10T17:06:51.513 回答
2

您需要JOIN表格,然后应用GROUP BY

select t1.name,
    t1.calls,
    sum(t1.hours + nz(t2.hours, 0)) as TotalHours,
    t1.calls / sum(t1.hours + nz(t2.hours, 0)) as CallsPerHour,
    t1.date
from table1 t1
left join table2 t2
    on t1.name = t2.name
    and t1.date = t2.date
group by t1.name, t1.calls, t1.date
于 2012-09-10T17:29:01.337 回答
1
SELECT Name, SUM(Calls) AS TotalCalls, SUM(Hours) AS TotalHours, 
    SUM(Calls) / SUM(Hours)  AS CallsPerHour, Date
FROM (
    SELECT Name, Calls, Hours, Date FROM Table1
    UNION ALL
    SELECT Name, 0, Hours, Date  FROM Table2
) BothTables
GROUP BY Name, Date
ORDER BY Date DESC, SUM(Calls) / SUM(Hours) DESC
于 2012-09-10T17:09:02.990 回答
1

Name由于两个表每个per只能有一行Date,因此您不需要GROUP BY查询。用于LEFT JOIN容纳TableTwo.

SELECT
    t1.Name, 
    t1.Calls,
    t1.Hours + Nz(t2.Hours,0) AS TotalHours,
    t1.Calls / (t1.Hours + Nz(t2.Hours,0)) AS CallsPerHour,
    t1.Date
FROM
    TableOne AS t1
    LEFT JOIN TableTwo AS t2
    ON
        t1.Name = t2.Name
    AND t1.Date = t2.Date;

使用您修改后的样本数据,这是该查询的结果集。

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.92307692307692  9/9/2012
Paul      50        6.5 7.69230769230769  9/9/2012
Cindy     20        2.5                8  9/9/2012
David     20          2               10  9/9/2012
于 2012-09-10T18:35:50.097 回答