Solved

How to show the last employee that updated a record

Posted on 2016-09-07
5
45 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 40

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 40

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server - Find Unmatched Records Between Two Tables - Has Trouble with Nulls 10 29
Query Syntax 17 34
MS SQL + Insert Into Table - If Doesnt Exist 9 34
Mysql Left Join Case 10 51
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
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
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

776 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