Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL SELECT: combining and grouping data between three tables using subqueries
    primarykey
    data
    text
    <p>Sorry for a long question and not a very descriptive title, but my problem is very difficult to explain briefly.</p> <p>I have three database tables:</p> <pre><code>TABLE A: AID PK STATUS VARCHAR TABLE B: BID PK AID FK CID FK TABLE C: CID PK CREATIONTIME DATE </code></pre> <p>For each STATUS = 'OK' row in table A I want to find the corresponding row in C which has the latest creation time.</p> <p>First I can to fetch all rows from table A where STATUS = 'OK'.<br> Next I can to fetch all corresponding rows from table B.<br> But how to continue from there?</p> <p>For example:</p> <pre><code>select AID, CID from B where AID in (select AID from A where STATUS = 'OK') </code></pre> <p>could return something like:</p> <pre><code>AID, CID 1 1 2 2 2 3 3 4 4 5 4 6 </code></pre> <p>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.</p> <p>Is there a way to express this with a query?</p> <p>EDIT: Sorry that I wasn't specific enough. What I want to get is the CIDs from table C.</p> <p>EDIT: I counted returned rows with the different solutions. Results were very interesting - and diversified:<br> HAINSTECH: 298 473 rows<br> JMUCCHIELLO: 298 473 rows<br> RUSS CAM: 290 121 rows<br> CHRIS: 344 093 rows<br> TYRANNOSAURS: 290 119 rows</p> <p>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.</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.
 

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