0

TurnTime が 4 番目のテーブルの DATEDIFF である状態で、3 つの異なるテーブルから列を取得しようとしています

CustomerName, OrderTypeName, LoanNumber, TurnTime1, TurnTime2,TurnTime3, TurnTime4

テーブル:

tblCustomer, tblOrderType, tblOrder, tblOrderActivity, tblActivity

DATEDIFF を配置すると、構文エラーが発生します。私はそれを非常に間違っていると思い、時間の違いを得ようとしています.

次のテーブルは、DATEDIFF の tblOrderActivity で使用されます。

tblActivity      ActivityID                ActivityName
                      1                      Received
                      2                       Keyed
                      3                      Printed
                      4                      Delivered

1-2、2-3、3-4、1-4 から DATEDIFF を見つけたいです。ActivityIDそしてActivityDate入っているtbl.OrderActivity

日付を取得したいテーブルにを入れようとしました(Select DATEDIFFが、構文エラーも発生しました。INNERJOINこれが私の現在のコードです:

SELECT   
dbo.tblCustomer.CustomerName, dbo.tblOrderType.OrderTypeName, dbo.tblOrder.LoanNumber, 
dbo.tblOrderActivity.ActivityDate, 
DATEDIFF(MINUTE, ActivityID=1, ActivityID=2) AS TurnTime1, 
DATEDIFF(MINUTE, ActivityID=2, ActivityID=3) AS TurnTime2,
DATEDIFF(MINUTE, ActivityID=3, ActivityID=4) AS TurnTime3,
DATEDIFF(MINUTE, ActivityID=1, ActivityID=4) AS TurnTime4
FROM     
dbo.tblOrder 
INNER JOIN
    dbo.tblCustomer ON dbo.tblOrder.CustomerID = dbo.tblCustomer.CustomerID 
INNER JOIN
    dbo.tblOrderActivity ON dbo.tblOrder.OrderID = dbo.tblOrderActivity.OrderID AND 
    dbo.tblOrder.OrderID = dbo.tblOrderActivity.OrderID 
INNER JOIN
    dbo.tblActivity ON dbo.tblOrderActivity.ActivityID = dbo.tblActivity.ActivityID 
INNER JOIN
    dbo.tblOrderType ON dbo.tblOrder.OrderTypeID = dbo.tblOrderType.OrderTypeID


ORDER BY dbo.tblCustomer.CustomerName, dbo.tblOrderType.OrderTypeName, dbo.tblOrder.LoanNumber
4

2 に答える 2

0

このようなもの ?

SELECT   
dbo.tblCustomer.CustomerName, dbo.tblOrderType.OrderTypeName, dbo.tblOrder.LoanNumber, 
a1.ActivityDate, 
DATEDIFF(MINUTE, a1.ActivityDate, a2.ActivityDate) AS TurnTime1, 
DATEDIFF(MINUTE, a2.ActivityDate, a3.ActivityDate) AS TurnTime2,
DATEDIFF(MINUTE, a3.ActivityDate, a4.ActivityDate) AS TurnTime3,
DATEDIFF(MINUTE, a1.ActivityDate, a4.ActivityDate) AS TurnTime4
FROM     
dbo.tblOrder 
INNER JOIN
    dbo.tblCustomer ON dbo.tblOrder.CustomerID = dbo.tblCustomer.CustomerID 
INNER JOIN
    dbo.tblOrderActivity a1 ON dbo.tblOrder.OrderID = a1.OrderID and a1.activityid = 1
INNER JOIN
    dbo.tblOrderActivity a2 ON dbo.tblOrder.OrderID = a2.OrderID and a1.activityid = 2
INNER JOIN
    dbo.tblOrderActivity a3 ON dbo.tblOrder.OrderID = a3.OrderID and a1.activityid = 3
INNER JOIN
    dbo.tblOrderActivity a4 ON dbo.tblOrder.OrderID = a4.OrderID and a1.activityid = 4
INNER JOIN
    dbo.tblOrderType ON dbo.tblOrder.OrderTypeID = dbo.tblOrderType.OrderTypeID
ORDER BY dbo.tblCustomer.CustomerName, dbo.tblOrderType.OrderTypeName, dbo.tblOrder.LoanNumber

ケチル

于 2013-05-16T10:24:53.560 に答える
0

最終的なコード Ketil にご協力いただきありがとうございます。これが私の問題に対する完全に機能し、機能する解決策です。

SELECT   
dbo.tblCustomer.CustomerName, dbo.tblOrderType.OrderTypeName, dbo.tblOrder.LoanNumber, 
DATEDIFF(MINUTE, a1.ActivityDate, a2.ActivityDate) AS [TurnTime1], 
DATEDIFF(MINUTE, a2.ActivityDate, a3.ActivityDate) AS [TurnTime2],
DATEDIFF(MINUTE, a3.ActivityDate, a4.ActivityDate) AS [TurnTime3],
DATEDIFF(MINUTE, a1.ActivityDate, a4.ActivityDate) AS [TurnTime4]
FROM     
dbo.tblOrder 
LEFT OUTER JOIN
dbo.tblCustomer ON dbo.tblOrder.CustomerID = dbo.tblCustomer.CustomerID 
LEFT OUTER JOIN
dbo.tblOrderActivity a1 ON dbo.tblOrder.OrderID = a1.OrderID and a1.ActivityID = 1
LEFT OUTER JOIN
dbo.tblOrderActivity a2 ON dbo.tblOrder.OrderID = a2.OrderID and a2.ActivityID = 2
LEFT OUTER JOIN
dbo.tblOrderActivity a3 ON dbo.tblOrder.OrderID = a3.OrderID and a3.ActivityID = 3
LEFT OUTER JOIN
dbo.tblOrderActivity a4 ON dbo.tblOrder.OrderID = a4.OrderID and a4.ActivityID = 4
INNER JOIN
dbo.tblOrderType ON dbo.tblOrder.OrderTypeID = dbo.tblOrderType.OrderTypeID
ORDER BY dbo.tblCustomer.CustomerName, dbo.tblOrderType.OrderTypeName, dbo.tblOrder.LoanNumber, a1.ActivityDate;
于 2013-05-20T05:52:33.567 に答える