1

bw 3 表的关系如下:

Table A

TXNID DATE AMT
123   11/1 230


Table A_Audit

TXNID Status      ChangeStatusID
123   SHIPPING     CHANGE001
123   IN-TRANSIT   CHANGE002
123   DELIVERED    CHANGE003

Table StatusChange

Status      ChangeStatusID  TimeStamp
SHIPPING     CHANGE001      Time1
IN-TRANSIT   CHANGE002      Time2
DELIVERED    CHANGE003      Time3

如果从发货到交付的状态变化超过 60 分钟,我需要从表 A 中获取 TxnID、日期和金额。

我尝试加入 A、A_Audit A_Audit 和状态更改(运输) A_Audit 和状态更改(已交付)

但这没有返回任何行。您能否就如何继续进行一些指示。

SELECT A.TXNID,A.DATE,A.AMT
FROM A A
JOIN A_Audit on A.TXNID = A_Audit.TXNID
JOIN StatusChange SC1 on A_Audit.ChangeStatusID=SC1.ChangeStatusID AND SC1.Status =    'Delivered'
JOIN StatusChange SC2 on A_Audit.ChangeStatusID=SC2.ChangeStatusID AND SC2.Status = 'Shipped'
WHERE datediff( 'mi', SC1.Timestamp, SC2.Timestamp )>60
4

2 回答 2

0
SELECT TXNID, "Date", AMT
FROM   A x
WHERE  EXISTS
       ( SELECT 1
         FROM   A_Audit a
                INNER JOIN
                StatusChange s
                ON (a.ChangeStatusID = s.ChangeStatusID AND a.Status = s.Status)
         WHERE  x.TXNID = a.TXNID
         GROUP BY a.TXNID
         HAVING (  MAX( DECODE( s.Status, 'DELIVERED', s."TimeStamp", null ) )
                 - MIN( DECODE( s.Status, 'SHIPPING',  s."TimeStamp", null ) ) )*24 > 1
       );

SQLFIDDLE

于 2013-11-14T22:16:35.027 回答
0

像这样的东西:

SELECT A.TXNID,MAX(A.DATE),MAX(AMT)
FROM A
JOIN A_Audit on A.TXNID = A_Audit.TXNID 
     AND (A_Audit.Status='SHIPPING' or A_Audit.Status='DELIVERED')
JOIN StatusChange on A_Audit.ChangeStatusID=StatusChange.ChangeStatusID
GROUP BY A.TXNID
HAVING datediff( 'mi', MIN(StatusChange.Time), MAX(StatusChange.Time) )>60
于 2013-11-14T13:50:57.280 回答