Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional 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 May 16th, 2011, 01:37 PM
Registered User
Join Date: Apr 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default Having trouble with sql statment.

Hello, I had posted in another thread but was having trouble getting a response. If anyone has the time please read on. If I'm going about this the wrong way please let me know.

I am using asp.net 2.0 with a sql database using sql server 2005 and have a table with 4 columns and am trying to make a filter for a pneumatic tube delivery system that is used where I work.

The table looks like this:

ID(key) TubeDestination TimeSent ContainsItems
1-------- 88 -------------20:00 -----yes
2-------- 88 -------------21:00 -----no
3-------- 88 -------------22:00 -----yes
4------- 100 -------------20:00 -----yes
5------- 100 -------------21:00 -----yes
6--------100 -------------22:00 -----yes
7--------150 -------------21:00 -----yes

I'd like to display the most recent tube destination and time sent for each tube station but only if it has been more than one hour since a tube has been sent to a location

For the above table if it was currently 23:30 I would hope for the below return:

TubeDestination TimeSent
--88------------- 22:00
--100------------ 22:00

I'm thought the following was right but then realized it will show the most recent tube destination and time that is greater than one hour but will show a that destination that was sent more than an hour ago even if a more recent tube was sent within the past hour.

SELECT TubeDestination, MAX(TimeSent) AS TimeSent
FROM Table1
WHERE TimeSent < dateadd(hh,-1,getdate())
GROUP BY TubeDestination

as an example...the 88 and 100 are right but the 150 is showing up and I don't want it to at all since there was a more recent tube sent at 2300.

TubeDestination TimeSent
--88------------- 22:00
--100------------ 22:00

Every nested query I use seems to end up using two columns which is a no-no. If anyone has some ideas or a solution I'd love the help. Thanks Dan
Old May 17th, 2011, 02:42 AM
Authorized User
Join Date: Jan 2011
Posts: 86
Thanks: 1
Thanked 12 Times in 12 Posts


don't shoot me if my example query doesn't work, as I'm more experienced with Oracle than Sql Server. But to me it looks like you need to use the NOT EXISTS clause. The logic is that you want to exclude any tubedestination where there was activity the last hour? Then I would suggest something like this:
SELECT TubeDestination, MAX(TimeSent) AS TimeSent
FROM Table1 t1
	not exists (
		FROM Table1 t2 
			t2.TubeDestination = t1.TubeDestination and 
			t2.TimeSent > dateadd(hh,-1,getdate())
GROUP BY TubeDestination
Hope this helps.

Last edited by disel2010; May 17th, 2011 at 02:45 AM.. Reason: grammatical correction

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with sql statment Dumb Dan SQL Language 1 May 6th, 2011 01:13 PM
Trouble returning most recent transaction with sql statment Dumb Dan ASP.NET 2.0 Professional 3 April 5th, 2011 10:25 PM
Using VB.Net to change the SQL Statment yicksang BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 March 31st, 2006 04:17 AM
SQL Statment btpoole VB Databases Basics 1 February 18th, 2004 01:03 PM
SQL statment arshad mahmood VB Databases Basics 1 July 1st, 2003 02:10 PM

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