1

これが現在の私のテーブルです。

ここに画像の説明を入力

従業員には、複数の注文日と出荷日があります。私が取得したいのは、各従業員の差分合計の平均です。

これを達成する方法に苦労しています。

これは私がこれまで行ってきたことです。

USE Northwind 

SELECT 
    e.EmployeeID, 
    e.LastName, 
    o.OrderDate, 
    o.ShippedDate, 
    DATEDIFF(DAY, o.OrderDate, o.ShippedDate) as Diff 
FROM 
    Employees as e 
    JOIN Orders as o ON e.EmployeeID = o.EmployeeID
4

2 に答える 2

4
SELECT 
    e.EmployeeID, 
    AVG(CAST(DATEDIFF(DAY, o.OrderDate, o.ShippedDate) as float)) as AvgDiff 
FROM 
    Employees as e 
    JOIN Orders as o ON e.EmployeeID = o.EmployeeID
Group By e.EmployeeID

EmployeeId結果セットに以上のものが必要な場合は、次のように参加し直してください。

select 
    e.EmployeeId
    e.LastName, 
    a.AvgDiff
From
    Employees as e 
Join     (SELECT 
              e.EmployeeID, 
              AVG(CAST(DATEDIFF(DAY, o.OrderDate, o.ShippedDate) as float)) as AvgDiff 
          FROM 
              Employees as e 
              JOIN Orders as o ON e.EmployeeID = o.EmployeeID
          Group By e.EmployeeID) a ON a.EmployeeId = e.EmployeeId
于 2012-11-14T07:13:56.230 に答える
3
SELECT 
    e.EmployeeID, 
    e.LastName, 
    AVG(1.0 * DATEDIFF(DAY, o.OrderDate, o.ShippedDate)) as AVGDiff 
FROM 
    Employees as e 
    JOIN Orders as o ON e.EmployeeID = o.EmployeeID
Group by  e.EmployeeID, e.LastName
Order by  e.EmployeeID, e.LastName 
于 2012-11-14T07:13:33.197 に答える