Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 205941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:36:04+00:00 2026-05-11T17:36:04+00:00

Sorry for a long question and not a very descriptive title, but my problem

  • 0

Sorry for a long question and not a very descriptive title, but my problem is very difficult to explain briefly.

I have three database tables:

TABLE A:  
AID PK  
STATUS VARCHAR

TABLE B:  
BID PK  
AID FK  
CID FK

TABLE C:  
CID PK  
CREATIONTIME DATE

For each STATUS = ‘OK’ row in table A I want to find the corresponding row in C which has the latest creation time.

First I can to fetch all rows from table A where STATUS = ‘OK’.
Next I can to fetch all corresponding rows from table B.
But how to continue from there?

For example:

select AID, CID from B where AID in (select AID from A where STATUS = 'OK')

could return something like:

AID, CID  
1    1  
2    2  
2    3  
3    4  
4    5  
4    6  

Let’s say that CID 2 has later creation time than CID 3 and CID 6 is newer than CID 5. This means that the correct result would be rows 1, 2, 4 and 6 in table C.

Is there a way to express this with a query?

EDIT:
Sorry that I wasn’t specific enough. What I want to get is the CIDs from table C.

EDIT:
I counted returned rows with the different solutions. Results were very interesting – and diversified:
HAINSTECH: 298 473 rows
JMUCCHIELLO: 298 473 rows
RUSS CAM: 290 121 rows
CHRIS: 344 093 rows
TYRANNOSAURS: 290 119 rows

I have not yet had the time to analyse returned rows in depth, but I’d really appreciate views on which of the queries are “broken” and why.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:36:05+00:00Added an answer on May 11, 2026 at 5:36 pm

    Something like this, if I’ve understood you correctly

    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
    

    EDIT:

    I have now checked the following in SQL Server (I would epxect the same outcome in Oracle) and it returns the CID for the C record with the Maximum CREATIONTIME where the STATUS for the related record in A id 'OK'.

    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
    

    Demonstrated with the following T-SQL

    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
    

    Results in the following

    CID
    -----------
    3
    4
    5
    

    EDIT 2:

    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

    --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);
    

    the results are as follows

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the long question title. I guess I'm on to a loser on
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry for long question, but I don't know how to make it shorter. 1.
Sorry in advance for the long question. What I'm really interested in is a
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for the second newbie question, I'm a developer not a sysadmin so this
Sorry if this sounds like a really stupid question, but I need to make
somewhat related to: libxml2 from java yes, this question is rather long-winded - sorry.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.