Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help With SQL Statement
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

2 Posts

Posted - 12/03/2012 :  11:13:01  Show Profile  Reply with Quote
Hello. I have the following table:

CREATE TABLE [dbo].[TempTable](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[TimeStamp] [datetime] NULL,
	[Direction] [nvarchar](5) NULL,
	[TimeOut] [datetime] NULL,
	[Name] [nvarchar](50) NULL)

When a person is checked into the system I create a record with a Name a current TimeStamp and TimeOut set to null. When a person is checked out of the system I create a record with a Name, a current TimeStamp and a current TimeOut.

My question is, what sql statement would generate all people currently checked into the system? Or is there a better way to handle this?

Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/03/2012 :  11:18:34  Show Profile  Reply with Quote
You can query like shown below. However, you should have some type of CustomerID that is unique to each custoemer at each check in because more than one person can have the same name (and one person can check in and check out multiple times, perhaps?)
FROM   [dbo].[TempTable] a
       SELECT *
       FROM   [dbo].[TempTable] b
       WHERE =
              AND b.Direction = 'OUT'
AND a.Direction = 'IN';
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 12/03/2012 :  11:19:40  Show Profile  Reply with Quote
yep. you should be updating already added record for checkout rather than creating a new record.
this way you could just get list of persons currently checked in using a simple query like

FROM TempTable

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000