sql - CTE to represent a logical table for the rows in a table which have the max value in one column -


I have a "only insert" database, in which the records are not physically updated, but rather logical forms by adding a new record With a CRUd value, with a large sequence, in this situation, the "CEC" column can consider a primary key, but the "ID" is the logical identifier for the record. In the example below,

This is a physical representation of the table:

 seq id name | CRUD | ---- | ----- | -------- | ------ | 1 | 10 | John | C. 2 | 10 | Joe You 3 | 11 | Kent | C. 4 | 12 | Kati | C. 5 | 12 | Sue | You 6 | 13. Jill | C. 7 | 14 | Bill | C. 

This is the logical representation of the table considering the "most recent" records:

 seq id name | CRUD | ---- | ----- | -------- | ------ | 2 | 10 | Joe You 3 | 11 | Kent | C. 5 | 12 | Sue | You 6 | 13. Jill | C. 7 | 14 | Bill | C. For example, for example, to recover the most recent record for the person with id = 12, I will do something like this currently: 

  People from PDP P WHERE PIID = 12 and p. S.E.K. = (P1 PE WHERE PID = 12) (select MAX (P1 SEEP)  

... and I will get this line:

I instead I want to do something:

  with NEW_P AS (- CTE represents all the most recent archives - for the given ID, the most recent sequence) SELECT * FROM NEW_P P2 WHERE P2 ID = 12  

Before using SQL, the first SQL example is already us

Question: How did the benefits of CTE be made to simplify our predictions when I needed to take advantage of the "most recent" logical approach of the table In short, I do not want to inline one subquery once upon a most recent record. I define a CTE and leverage that is any later in the forecast.

Ps when i work I am using DB2 in Titan, then I am looking for a solution that is database based on atheist.

This window (or muff ) functions Is a clear case for all those supported by the modern SQL database. For example:

 with  as ORD_P (SELECT p. *, ROW_NUMBER ()) (segment by people ID segment, p), NEW_P AS (SELECT * ORD_P WHERE = 1) SELECT * to NEW_P P2 WHERE P2.ID = 12  

PS not tested. You may need to list all the columns explicitly in the CTE section.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -