0

尝试从 3 个不同的表中检索列,TurnTime 是来自第 4 个表的 DATEDIFF

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

表:

tblCustomer, tblOrderType, tblOrder, tblOrderActivity, tblActivity

放置 DATEDIFF 时遇到语法错误。我认为我做错了,并试图在时代有所不同。

下表在 tblOrderActivity 中用于 DATEDIFF

tblActivity      ActivityID                ActivityName
                      1                      Received
                      2                       Keyed
                      3                      Printed
                      4                      Delivered

我想从 1-2、2-3、3-4、1-4 中找到 DATEDIFF。ActivityID并且ActivityDatetbl.OrderActivity

我试图将一个(Select DATEDIFFINNERJOIN我希望从中检索日期的表放在一起,但是,它也导致了语法错误。这是我当前的代码:

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

Thanks for your assistance in the final code Ketil. Here is the full functioning and working solution to my problem.

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 回答
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 回答