473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

get the latest changed records

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
1 958
amitpatel66
2,367 Expert 2GB
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

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

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
9
by: Gleep | last post by:
sorry i didn't explain it correctly before my table is like this example fields: ID name username outcome date1 date2 date3 (etc..) - date15 price1 price2 price3 (etc..) I know that...
4
by: Henry | last post by:
Dear all, I am now designing a table for storing inventory of servers in a DB2 DB. However, I am new to SQL and would like to seek advice whether the design could work as expect with DB2 SQL. ...
3
by: Kyle Keller | last post by:
I created a database which my company has used for over 2 years at now over 30 locations. Just today, one of the locations called and was having problems with their program. The database is A97...
2
by: M.Stanley | last post by:
Hi, I have a problem..I'm doing a specific query where I'm joining fields from a table with appednded data (there are duplicate records, except for the date/time), and another query. I want the...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
8
by: TGEAR | last post by:
I have an ItemSTDPriceHistory table as below and this is a child table of itemlookup table with one to many relationship. if exists (select * from dbo.sysobjects where id = object_id(N'.') and...
4
by: TGEAR | last post by:
Itemlookup table Field names : index_id (primary key), itemno, description. It has a child table, which is ItemPriceHistory table The relationship to the child table is one (parent table)-to-many...
1
by: Alan | last post by:
Hello World: I have a new task at work. I have to process a mainframe report (a text file), and put it into an Access 2003 database. When I get the report, it looks like this: Account Sub_Name...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.