Solved

How to show the last employee that updated a record

Posted on 2016-09-07
5
48 Views
Last Modified: 2016-09-07
I want to show only the last "EMPLOYEE" that updated in the record. In other words, the last employee date stamp in field dbo.WOLABO01B.date.

SELECT     TOP (100) PERCENT dbo.WOMAST01B.step, dbo.WOMAST01B.wono AS WO, dbo.WOMAST01B.item AS PN, dbo.WOMAST01B.location, 
                      dbo.WOMAST01B.dept AS SHOP, dbo.WOLABO01B.prempl AS EMPLOYEE, SUM(dbo.WOLABO01B.time) AS TIME, DATEDIFF(dd, 
                      dbo.WOMAST01B.recdate, GETDATE()) AS [DAYS LEFT], dbo.WOMAST01B.tat
FROM         dbo.WOMAST01B LEFT OUTER JOIN
                      dbo.WOLABO01B ON dbo.WOMAST01B.wono = dbo.WOLABO01B.wono
WHERE     (dbo.WOMAST01B.dept = 'AVI')
GROUP BY dbo.WOMAST01B.step, dbo.WOMAST01B.wono, dbo.WOMAST01B.item, dbo.WOMAST01B.location, dbo.WOMAST01B.dept, 
                      dbo.WOMAST01B.location2, dbo.WOLABO01B.prempl, DATEDIFF(dd, dbo.WOMAST01B.recdate, GETDATE()), dbo.WOMAST01B.tat
ORDER BY [DAYS LEFT] DESC

Open in new window

Capture.PNG
0
Comment
Question by:maximus1974
  • 2
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
Russell Fox earned 500 total points
ID: 41788266
You can use a subquery:
SELECT TOP (100) PERCENT 
	woma.step
	, woma.wono AS WO
	, woma.item AS PN
	, woma.location
	, woma.dept AS SHOP
	, dbo.WOLABO01B.prempl AS EMPLOYEE
	, SUM(dbo.WOLABO01B.time) AS TIME
	, DATEDIFF(dd, woma.recdate, GETDATE()) AS [DAYS LEFT]
	, woma.tat
	, LastDate = (SELECT TOP 1 [date] FROM dbo.WOLABO01B WHERE wono = woma.wono WHERE [date] IS NOT NULL ORDER BY [date] DESC)
FROM dbo.WOMAST01B woma
	LEFT OUTER JOIN dbo.WOLABO01B wola
		ON woma.wono = wola.wono
WHERE woma.dept = 'AVI'
GROUP BY woma.step
	, woma.wono
	, woma.item
	, woma.location
	, woma.dept
	, woma.location2
	, wola.prempl
	, DATEDIFF(dd, woma.recdate, GETDATE())
	, woma.tat
ORDER BY [DAYS LEFT] DESC

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 41788375
Can you provide your expected result?
0
 

Author Comment

by:maximus1974
ID: 41788406
is this statement correct?

LastDate = (SELECT TOP 1 [date] FROM dbo.WOLABO01B WHERE wono = woma.wono WHERE [date] IS NOT NULL ORDER BY [date] DESC)
FROM dbo.WOMAST01B woma

Getting syntax errors.
0
 

Author Comment

by:maximus1974
ID: 41788407
Expected result is no WONO duplicates and one employee which is the last one to clock in.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41788545
In your SELECT clause, I don't see any DATE column but in your screenshot, you have DATE as the last column after tat. Which column is displaying the DATE in your screenshot.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
monitoring configuration for SQL server DB 32 45
Freeze portion of datamart 2 21
Return Rows as per Quantity of Columns Value In SQL 6 26
Not listening to where 1 12
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

685 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