SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

jbbatts77
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?

sunitabeck
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?)
SELECT *
FROM   [dbo].[TempTable] a
WHERE  NOT EXISTS 
(
       SELECT *
       FROM   [dbo].[TempTable] b
       WHERE  a.name = b.name
              AND b.Direction = 'OUT'
)
AND a.Direction = 'IN';
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 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

SELECT *
FROM TempTable
WHERE TimeOut IS NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000