Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Something like this, if I've understood you correctly</p> <pre><code>SELECT MAX(CREATIONTIME), A.AID FROM A INNER JOIN B ON A.AID = B.AID INNER JOIN C ON B.CID = C.CID WHERE A.STATUS = 'OK' GROUP BY A.AID </code></pre> <p><strong>EDIT:</strong></p> <p>I have now checked the following in SQL Server (I would epxect the same outcome in Oracle) and it returns the <code>CID</code> for the <code>C</code> record with the Maximum <code>CREATIONTIME</code> where the <code>STATUS</code> for the related record in <code>A</code> id <code>'OK'</code>.</p> <pre><code>SELECT C.CID FROM C C INNER JOIN B B ON C.CID = B.CID INNER JOIN ( SELECT MAX(C.CREATIONTIME) CREATIONTIME, A.AID FROM A A INNER JOIN B B ON A.AID = B.AID INNER JOIN C C ON B.CID = C.CID WHERE A.STATUS = 'OK' GROUP BY A.AID ) ABC ON B.AID = ABC.AID AND C.CREATIONTIME = ABC.CREATIONTIME </code></pre> <p>Demonstrated with the following <strong><em>T-SQL</em></strong></p> <pre><code>DECLARE @A TABLE(AID INT IDENTITY(1,1), STATUS VARCHAR(10)) DECLARE @B TABLE(BID INT IDENTITY(1,1), AID INT, CID INT) DECLARE @C TABLE(CID INT IDENTITY(1,1), CREATIONTIME DATETIME) INSERT INTO @A VALUES ('OK') INSERT INTO @A VALUES ('OK') INSERT INTO @A VALUES ('NOT OK') INSERT INTO @A VALUES ('OK') INSERT INTO @A VALUES ('NOT OK') INSERT INTO @C VALUES ('10 MAR 2008') INSERT INTO @C VALUES ('13 MAR 2008') INSERT INTO @C VALUES ('15 MAR 2008') INSERT INTO @C VALUES ('17 MAR 2008') INSERT INTO @C VALUES ('21 MAR 2008') INSERT INTO @B VALUES (1,1) INSERT INTO @B VALUES (1,2) INSERT INTO @B VALUES (1,3) INSERT INTO @B VALUES (2,2) INSERT INTO @B VALUES (2,3) INSERT INTO @B VALUES (2,4) INSERT INTO @B VALUES (3,3) INSERT INTO @B VALUES (3,4) INSERT INTO @B VALUES (3,5) INSERT INTO @B VALUES (4,5) INSERT INTO @B VALUES (4,1) INSERT INTO @B VALUES (4,2) SELECT C.CID FROM @C C INNER JOIN @B B ON C.CID = B.CID INNER JOIN ( SELECT MAX(C.CREATIONTIME) CREATIONTIME, A.AID FROM @A A INNER JOIN @B B ON A.AID = B.AID INNER JOIN @C C ON B.CID = C.CID WHERE A.STATUS = 'OK' GROUP BY A.AID ) ABC ON B.AID = ABC.AID AND C.CREATIONTIME = ABC.CREATIONTIME </code></pre> <p>Results in the following</p> <pre><code>CID ----------- 3 4 5 </code></pre> <p><strong>EDIT 2:</strong></p> <p>In response to your comment about each of the statements giving different results, I have ran some of the different answers here through SQL Server 2005 using my test data above (I appreciate you are using Oracle). Here are the results</p> <pre><code>--Expected results for CIDs would be --CID ----------- --3 --4 --5 --As indicated in the comments next to the insert statements DECLARE @A TABLE(AID INT IDENTITY(1,1), STATUS VARCHAR(10)) DECLARE @B TABLE(BID INT IDENTITY(1,1), AID INT, CID INT) DECLARE @C TABLE(CID INT IDENTITY(1,1), CREATIONTIME DATETIME) INSERT INTO @A VALUES ('OK') -- AID 1 INSERT INTO @A VALUES ('OK') -- AID 2 INSERT INTO @A VALUES ('NOT OK') INSERT INTO @A VALUES ('OK') -- AID 4 INSERT INTO @A VALUES ('NOT OK') INSERT INTO @C VALUES ('10 MAR 2008') INSERT INTO @C VALUES ('13 MAR 2008') INSERT INTO @C VALUES ('15 MAR 2008') INSERT INTO @C VALUES ('17 MAR 2008') INSERT INTO @C VALUES ('21 MAR 2008') INSERT INTO @B VALUES (1,1) INSERT INTO @B VALUES (1,2) INSERT INTO @B VALUES (1,3) -- Will be CID 3 For AID 1 INSERT INTO @B VALUES (2,2) INSERT INTO @B VALUES (2,3) INSERT INTO @B VALUES (2,4) -- Will be CID 4 For AID 2 INSERT INTO @B VALUES (3,3) INSERT INTO @B VALUES (3,4) INSERT INTO @B VALUES (3,5) INSERT INTO @B VALUES (4,5) -- Will be CID 5 FOR AID 4 INSERT INTO @B VALUES (4,1) INSERT INTO @B VALUES (4,2) -- Russ Cam SELECT C.CID, ABC.CREATIONTIME FROM @C C INNER JOIN @B B ON C.CID = B.CID INNER JOIN ( SELECT MAX(C.CREATIONTIME) CREATIONTIME, A.AID FROM @A A INNER JOIN @B B ON A.AID = B.AID INNER JOIN @C C ON B.CID = C.CID WHERE A.STATUS = 'OK' GROUP BY A.AID ) ABC ON B.AID = ABC.AID AND C.CREATIONTIME = ABC.CREATIONTIME -- Tyrannosaurs select A.AID, max(AggC.CREATIONTIME) from @A A, @B B, ( select C.CID, max(C.CREATIONTIME) CREATIONTIME from @C C group by CID ) AggC where A.AID = B.AID and B.CID = AggC.CID and A.Status = 'OK' group by A.AID -- jmucchiello SELECT c.cid, max(c.creationtime) FROM @B b, @C c WHERE b.cid = c.cid AND b.aid IN (SELECT a.aid FROM @A a WHERE status = 'OK') GROUP BY c.cid -- hainstech SELECT agg.aid, agg.cid FROM ( SELECT a.aid ,c.cid ,max(c.creationtime) as maxcCreationTime FROM @C c INNER JOIN @B b ON b.cid = c.cid INNER JOIN @A a on a.aid = b.aid WHERE a.status = 'OK' GROUP BY a.aid, c.cid ) as agg --chris SELECT A.AID, C.CID, C.CREATIONTIME FROM @A A, @B B, @C C WHERE A.STATUS = 'OK' AND A.AID = B.AID AND B.CID = C.CID AND C.CREATIONTIME = (SELECT MAX(C2.CREATIONTIME) FROM @C C2, @B B2 WHERE B2.AID = A.AID AND C2.CID = B2.CID); </code></pre> <p>the results are as follows</p> <pre><code>--Russ Cam - Correct CIDs (I have added in the CREATIONTIME for reference) CID CREATIONTIME ----------- ----------------------- 3 2008-03-15 00:00:00.000 4 2008-03-17 00:00:00.000 5 2008-03-21 00:00:00.000 --Tyrannosaurs - No CIDs in the resultset AID ----------- ----------------------- 1 2008-03-15 00:00:00.000 2 2008-03-17 00:00:00.000 4 2008-03-21 00:00:00.000 --jmucchiello - Incorrect CIDs in the resultset cid ----------- ----------------------- 1 2008-03-10 00:00:00.000 2 2008-03-13 00:00:00.000 3 2008-03-15 00:00:00.000 4 2008-03-17 00:00:00.000 5 2008-03-21 00:00:00.000 --hainstech - Too many CIDs in the resultset, which CID has the MAX(CREATIONTIME) for each AID? aid cid ----------- ----------- 1 1 1 2 1 3 2 2 2 3 2 4 4 1 4 2 4 5 --chris - Correct CIDs, it is the same SQL as mine AID CID CREATIONTIME ----------- ----------- ----------------------- 1 3 2008-03-15 00:00:00.000 2 4 2008-03-17 00:00:00.000 4 5 2008-03-21 00:00:00.000 </code></pre> <p>I would recommend running each of the given answers against a smaller number of records, so that you can ascertain whether the resultset returned is the expected one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload