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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help With SQL Statement

Author  Topic 

jbbatts77
Starting Member

2 Posts

Posted - 2012-12-03 : 11:13:01
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-03 : 11:18:34
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

52326 Posts

Posted - 2012-12-03 : 11:19:40
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
   

- Advertisement -