IF object_id('tempdb..#A') IS NOT NULL DROP TABLE #A
CREATE TABLE #A (Computer varchar(20), Name varchar(20), Month varchar(20))
INSERT INTO #A
SELECT 'PC1', 'Bob', 'June'
UNION ALL
SELECT 'PC1', 'Tammy', 'January'
UNION ALL
SELECT 'PC1', 'Wes', 'September'
UNION ALL
SELECT 'PC1', 'Susan', 'October'
UNION ALL
SELECT 'PC1', 'Kevin', 'February'
SELECT * FROM #A
This produces the results:
Computer Name Month
PC1 Bob June
PC1 Tammy January
PC1 Wes September
PC1 Susan October
PC1 Kevin February
The month column is the most popular user that has logged in on this computer in that month.
I would like to display the most recently logged in user, but they may not have logged in this month or even last month.
In the above example, if today is in June I would want to select PC1 Kevin.
So really, I would want a function to return a number 1-12 for a specified month relative to the current month.
If today is June 26, then
udf_month('June') would return 12
udf_month('May') would return 11
udf_month('January') would return 7
udf_month('December') would return 6
udf_month('July') would return 1
If today was July 1st, then udf_month('June') would return 11
Then I could do a SELECT Computer, Name, Max(udf_Month(Month)) to find the most recent Name for each Computer.
Correct?