2013-05-16 2 views
0
에서 여러 테이블 및 DATEDIFF의 반환 열이

는 4 테이블에서 TurnTime의 존재의 DATEDIFF 년대로, 3 개 개의 다른 테이블에서 열을 검색하려고SQL : 분

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

테이블 :

tblCustomer, tblOrderType, tblOrder, tblOrderActivity, tblActivity

DATEDIFF를 배치 할 때 구문 오류가 발생합니다. 나는 그것을 매우 잘못하고 있다고 생각하고 시대의 차이를 얻으려고 노력하고 있습니다.

다음 표는 내가 1-2, 2-3, 3-4, 1-4에서 DATEDIFF를 찾으려면 DATEDIFF

tblActivity  ActivityID    ActivityName 
         1      Received 
         2      Keyed 
         3      Printed 
         4      Delivered 

에 대한 tblOrderActivity에 사용됩니다. ActivityIDActivityDatetbl.OrderActivity

에 내가 그러나, 그것은 또한 오류를 구문을 주도, 난에서 날짜를 검색하고자하고있는 테이블의 INNERJOIN(Select DATEDIFF을 넣어 시도합니다. 여기에 현재 코드가 있습니다 :

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

이런 식으로 뭔가요? 최종 코드 Ketil에 도움을

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

+0

실행중인 것으로 보이지만 열의 행은 반환되지 않습니다. – Brody

0

감사합니다. 여기 내 문제에 대한 완전한 기능 및 작동 해결책이 있습니다.

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;