Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MSSQL - How to count records & select max timestamp?

Posted on 2009-02-12
6
Medium Priority
?
1,915 Views
Last Modified: 2012-05-06
I've got a table that looks like this:

Id | Uid | Value | Timestamp
1 | 65 | B | 2009-01-01 12:01:00.100
2 | 65 | B | 2009-01-02 12:01:00.100
3 | 65 | B | 2009-01-03 12:01:00.100
4 | 65 | B | 2009-01-04 12:01:00.100
5 | 65 | B | 2009-01-05 12:01:00.100

I'm trying to count the records that have the same Uid and Value, while returning the Uid, Value, and maximum timestamp like this:

Id | Uid | Value | Timestamp | counter
1 | 65 | B | 2009-01-05 12:01:00.100 | 5

I keep running into group by errors.  Is there a way to do this?
0
Comment
Question by:jen_jen_jen
6 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 23622165
try this:

select id = min(id), uid, value, max(timestamp),counter = count(*)
from tablename
group by uid, value
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 23622167
Hello jen_jen_jen,

SELECT MIN(Id) AS Id, Uid, Value, MAX(Timestamp) AS Timestamp, COUNT(*) AS Counter
FROM SomeTable
GROUP BY Uid, Value

Regards,

Patrick
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 23622173
Guess I need more coffee :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:momi_sabag
ID: 23622178
if you are using sql 2005 then

select id, uid, value, max(timestamp) over (partition by id, uid) as timestamp, count(*) over (partition by id, uid) as counter
from your table
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 23622286
MIN(id) will not work -- you need the id that corresponds to the rwo with the MAX(timestamp), not just the lowest id in the table.
0
 

Author Closing Comment

by:jen_jen_jen
ID: 31546055
Thanks! Never would have figured that out....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question