473,412 Members | 2,003 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,412 software developers and data experts.

SQL Question: Query for Newest Records

Forgive me if this is the wrong group to post to.

I have a table and the relevant bits look like this:

CREATE TABLE "records" (
"id" SERIAL,
"number" integer NOT NULL,
"timestamp" timestamp with time zone NOT NULL,
CONSTRAINT "records_pkey" PRIMARY KEY ("id")
);

Let's say there's 500 rows, and in the 500 rows the "number" column will
have any one of the values 1 to 10. Every value 1 to 10 is covered by
at least one row. The "timestamp" field has any timestamp value, up to
now. How can I get the newest "id" for each value in number?

If I do:
SELECT num,MAX(timestamp) FROM records GROUP BY num;
I get results like:
num | max
---------------
0 | 2004-04-01 03:02:01-05
1 | 2004-03-29 13:22:04-05
2 | 2001-12-11 01:05:12-05
....
8 | 1999-11-07 12:32:22-05
9 | 2001-02-11 14:52:33-05
10 | 2002-06-22 14:34:44-05

I need the "id" that corresponds to those maximum rows.

I realize this can be achieved through a couple of queries and some
programming magic, but I get the feeling that this is something that SQL
can do for me in one query.

Any insight would be appreciated.

Thanks,
Ryan.
Nov 23 '05 #1
0 2667

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
2
by: JeffM | last post by:
I have the following query: SELECT TOP 100 PERCENT Company_Code, Well_Code, Effective_Date, Interest FROM dbo.HUD_Expense_Interest_Group1 I need to have the query return one row...
20
by: Jack Schitt | last post by:
I thought I was starting to get a handle on Access, until I tried doing something useful...now I'm stuck. I have a DB with two tables - to keep it simple I'll say that one is an Employee File...
5
by: Bob | last post by:
Hi Everybody Difficult question Has anyone else used the "Using the Tab control as a container" database example that comes with the above book chapter 7 on the accompanying disc. It is a...
6
by: Dan V. | last post by:
Is there a way to query a remote xml file periodically by not using web services? For Windows and Unix platforms. Is there a cheap software product that I can install on each client and my Windows...
5
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
1
by: Rebeccar | last post by:
I am a self taught access user who muddles along! I have a database which has two tables one for recording staff details and the other to record their contract details - they are linked by their...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.