By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

get the latest changed records

P: 4
Hi,

I hava a table with the following information
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE TEMP1 (REFID INT, REVISION INT, FIELDNAM VARCHAR(10),  VALUE VARCHAR(10));
  2. INSERT INTO TEMP1 VALUES(1001, 0, 'A', 'A2');
  3. INSERT INTO TEMP1 VALUES(1001, 0, 'C', 'C2');
  4. INSERT INTO TEMP1 VALUES(1001, 0, 'E', 'E2');
  5. INSERT INTO TEMP1 VALUES(1002, 0, 'A', 'A3');
  6. INSERT INTO TEMP1 VALUES(1002, 0, 'B', 'B2');
  7. INSERT INTO TEMP1 VALUES(1002, 0, 'E', 'E3');
  8. INSERT INTO TEMP1 VALUES(1001, 1, 'A', 'A4');
  9. INSERT INTO TEMP1 VALUES(1001, 1, 'E', 'E4');
  10.  
Here based on latest revision and refid I should get the fieldnam and value.
Expected output:
REFID FIELDNAM VALUE REVISION
1001 A A4 1
1001 E E4 1
1002 B B2 0
1001 C C2 0
Feb 13 '08 #1
Share this Question
Share on Google+
1 Reply


amitpatel66
Expert 100+
P: 2,367
Hi,

I hava a table with the following information
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE TEMP1 (REFID INT, REVISION INT, FIELDNAM VARCHAR(10),  VALUE VARCHAR(10));
  2. INSERT INTO TEMP1 VALUES(1001, 0, 'A', 'A2');
  3. INSERT INTO TEMP1 VALUES(1001, 0, 'C', 'C2');
  4. INSERT INTO TEMP1 VALUES(1001, 0, 'E', 'E2');
  5. INSERT INTO TEMP1 VALUES(1002, 0, 'A', 'A3');
  6. INSERT INTO TEMP1 VALUES(1002, 0, 'B', 'B2');
  7. INSERT INTO TEMP1 VALUES(1002, 0, 'E', 'E3');
  8. INSERT INTO TEMP1 VALUES(1001, 1, 'A', 'A4');
  9. INSERT INTO TEMP1 VALUES(1001, 1, 'E', 'E4');
  10.  
Here based on latest revision and refid I should get the fieldnam and value.
Expected output:
REFID FIELDNAM VALUE REVISION
1001 A A4 1
1001 E E4 1
1002 B B2 0
1001 C C2 0
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT x.refid, x.fieldnam, t.value, x.revision FROM
  3. (SELECT MAX(revision),refid,fieldnam FROM temp1 GROUP by refid,fieldnam) x, temp1 t
  4. WHERE t.refid = x.refid
  5. AND t.revision = x.revision
  6. AND t.fieldnam = x.fieldnam
  7.  
  8.  
Feb 13 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.