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 July 13th, 2011, 12:23 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default Most Recent, Min and Max Values

Hello All,

I have to come up with a query that will pull out the most recent value within a two hour time frame from the current time. If the value exists then I need to pull out the minimum and maximum values within a 24 hour window from the time the most recent value was charted. The most recent value could be the minimum or maximum value too. Is there a way to do it with one query. I can do it with two queries.

Is there a way to display everything on one row for simplicity?

So if a value is charted at 11:00 like in the example, I need to pull the min and max values from 11:00 today looking back to yesterday at 11:00.

Thanks,
Tony


--------------------------------------------
USE TempDB
GO

CREATE TABLE dbo.labdata
(
ClientID INT,
TestDate DATETIME,
TestName VARCHAR(100),
TestValue INT
)

INSERT INTO LabData
(ClientID,TestDate,TestName,TestValue)
SELECT '1234','07/13/2011 01:00:00','Glucose', '100' UNION ALL
SELECT '1234','07/13/2011 02:00:00','Glucose', '200' UNION ALL
SELECT '1234','07/13/2011 03:00:00','Glucose', '300' UNION ALL
SELECT '1234','07/13/2011 11:00:00','Glucose', '400' UNION ALL
SELECT '2345','07/13/2011 05:00:00','BUN', '10' UNION ALL
SELECT '2345','07/13/2011 06:00:00','BUN', '30' UNION ALL
SELECT '2345','07/13/2011 07:00:00','BUN', '20' UNION ALL
SELECT '2345','07/13/2011 11:00:00','BUN', '15' UNION ALL
SELECT '3456','07/12/2011 01:00:00','CREATININE', '0.5' UNION ALL
SELECT '3456','07/13/2011 11:00:00','CREATININE', '0.6'
----------------------------------
Expected results:
1234 07/13/2011 11:00:00 Glucose 400 (Most recent value)
1234 07/13/2011 01:00:00 Glucose 100 (Min Value)
1234 07/13/2011 11:00:00 Glucose 400 (Max Value)

2345 07/13/2011 11:00:00 BUN 15 (The most recent value)
2345 07/13/2011 05:00:00 BUN 10 (Min Value)
2345 07/13/2011 06:00:00 BUN 30 (Max Value)

3456 07/13/2011 11:00:00 CREATININE 0.6 (Most recent value)
3456 07/13/2011 11:00:00 CREATININE 0.6 (Min Value)
3456 07/13/2011 11:00:00 CREATININE 0.6 (Max Value)
 
Old July 13th, 2011, 01:27 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Post your two queries. Let's see what you got.
 
Old July 14th, 2011, 01:50 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default

This request was canceled. Thank you.

Tony





Similar Threads
Thread Thread Starter Forum Replies Last Post
How does one get the Max or Min Filename? crabjoe ASP.NET 1.0 and 1.1 Basics 4 January 15th, 2008 04:48 PM
MAX/MIN Value Help slbibs SQL Server 2005 4 November 6th, 2007 04:00 AM
SQL script for selecting min and max values Nancy Krause Need help with your homework? 0 April 12th, 2007 11:29 AM
MIN( MAX( UPDATE Stuart Stalker SQL Language 3 February 21st, 2007 07:10 AM
How to set of Min/Max values of Line chart . Sanjay.Verma BOOK: Professional Crystal Reports for VS.NET 0 November 20th, 2006 11:35 PM





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