Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to show the last employee that updated a record

Posted on 2016-09-07
5
Medium Priority
?
59 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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