Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 13th, 2011, 12:23 PM
Authorized User
Points: 336, Level: 6
Points: 336, Level: 6 Points: 336, Level: 6 Points: 336, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2006
Location: , , .
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)
Reply With Quote
  #2 (permalink)  
Old July 13th, 2011, 01:27 PM
Friend of Wrox
Points: 2,101, Level: 18
Points: 2,101, Level: 18 Points: 2,101, Level: 18 Points: 2,101, Level: 18
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: , , .
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Post your two queries. Let's see what you got.
Reply With Quote
  #3 (permalink)  
Old July 14th, 2011, 01:50 PM
Authorized User
Points: 336, Level: 6
Points: 336, Level: 6 Points: 336, Level: 6 Points: 336, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2006
Location: , , .
Posts: 73
Thanks: 6
Thanked 0 Times in 0 Posts
Default

This request was canceled. Thank you.

Tony
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:25 AM.


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