[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Select last occurrence of a date(s)

Posted on 2009-12-16
4
Medium Priority
?
361 Views
Last Modified: 2012-05-08
Need to return select list containing each [Equip#]  where the last date entered is [CheckOutDate],
or that is those items currently checked out.

See Code Pane.
Table structure;
CREATE TABLE [dbo].[EquipmentHistory](
	[Rec#] [int] IDENTITY(1,1) NOT NULL,
	[Receipt#] [int] NULL,
	[Equip#] [nvarchar](50) NULL,
	[Qty] [int] NULL DEFAULT ((0)),
	[Emp#] [varchar](6)  DEFAULT (''),
	[CheckOutDate] [datetime] NULL,
	[CheckInDate] [datetime] NULL,
) ON [PRIMARY] 
Sample Data;
Rec#        EMP#   Equip#                                             CheckOutDate            CheckInDate             Check
----------- ------ -------------------------------------------------- ----------------------- ----------------------- -----
137890      1145   4854                                               2009-10-26 10:29:01.680 NULL                    OUT
137811      1145   4855                                               NULL                    2009-10-24 11:42:36.283 IN
137809      1145   4855                                               2009-10-24 11:39:02.860 NULL                    OUT
137777      1145   5055                                               NULL                    2009-10-23 19:20:37.820 IN
137760      1145   5055                                               2009-10-23 16:19:01.937 NULL                    OUT
137465      1145   4949                                               2009-10-22 08:04:13.933 NULL                    OUT
136832      1145   5919                                               NULL                    2009-10-16 11:10:19.957 IN
136831      1145   6025                                               NULL                    2009-10-16 11:10:13.113 IN
136830      1145   6021                                               NULL                    2009-10-16 11:10:06.063 IN
136483      1145   4843                                               NULL                    2009-10-14 15:59:50.610 IN
136482      1145   4477                                               NULL                    2009-10-14 15:59:32.877 IN
136472      1145   4843                                               2009-10-14 15:12:45.610 NULL                    OUT
136471      1145   4477                                               2009-10-14 15:12:34.063 NULL                    OUT
136418      1145   4482                                               2009-10-14 10:54:25.340 NULL                    OUT
136325      1145   4126                                               NULL                    2009-10-14 07:43:49.467 IN
136324      1145   6166                                               NULL                    2009-10-14 07:43:32.357 IN
136206      1145   4126                                               2009-10-13 12:47:26.793 NULL                    OUT
136205      1145   6166                                               2009-10-13 12:47:19.887 NULL                    OUT

Open in new window

0
Comment
Question by:Greg Rowland
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 26065311
not sure what exactly you are looking for

SELECT *
from EqupmentHistory e
Where  CheckOutDate = (SELECT Max(CheckOutDate) from EquipmentHistory WHERE [Equip#] =  e. [Equip#]  AND  [Check] = 'Out' )
0
 
LVL 4

Author Comment

by:Greg Rowland
ID: 26065477
aneeshattingal:

This query returns only the last record. What I  am shooting for is; each Equip# that has been checked out and not yet checked in by a specified Emp#.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 26065736
SELECT *
from EqupmentHistory e
Where  [Check] = 'Out'  and not exists (SELECT 1 from EquipmentHistory WHERE [Equip#] =  e.[Equip#] and CheckInDate IS not null     )
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question