尝试从3个不同的表中检索列,其中TurnTime是来自第4个表的DATEDIFF
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
答案 0 :(得分: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
Ketil
答案 1 :(得分: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;