Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2007, 09:07 PM
Authorized User
 
Join Date: Dec 2006
Posts: 70
Thanks: 0
Thanked 1 Time in 1 Post
Default I need help writing a SQL statement

I have an Equipment history table.
The primary key on the table is a composite key: ESN, StatusID, and date_insert.
I need to write a statement that will give me the last record for each ESN.
I am considering adding a column to the table to sequence the records, but I haven't done that yet.
Until then, can anyone help me write the where clause that will allow me to select the distinct records I need to report on?

If this isn't the correct forum, please let me know where I should post it.

Thanks in advance.
Karen

 
Old June 11th, 2007, 06:34 AM
Authorized User
 
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default

Try it like this:
Code:
SELECT ESN, MAX(date_insert) FROM Equipment GROUP BY ESN
Best regards,
Aleksandar

Programmer/Application developer
Belgrade, Serbia
 
Old June 12th, 2007, 08:54 AM
Authorized User
 
Join Date: Dec 2006
Posts: 70
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks.
I ended up creating a roll-up table containing the detailed information from the history table that I need to report on. It seems to work well for my application.
However, I will keep your suggestion in mind.

Karen

 
Old June 24th, 2007, 02:43 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT ESN, StatusID, Date_Insert
FROM (
SELECT ESN, StatusID, Date_Insert, ROW_NUMBER() OVER (PARTITION BY ESN ORDER BY Date_Insert DESC) AS RecID) AS d
WHERE RecID = 1







Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing DLL fom Report Rendering(SQL Server Repor) Somesh Reporting Services 0 December 31st, 2007 07:26 AM
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
Help writing SQL Statement/ .net code for function carswelljr Classic ASP Databases 2 August 24th, 2006 03:31 PM
writing SQL queries in MS access,VBA NovieProgrammer Access VBA 2 April 2nd, 2005 07:15 PM
Help writing a SQL sac11585 SQL Server 2000 2 October 6th, 2004 02:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.