Reputation: 99
I am trying to create a query that will get results of last software usage time per device.
In the table there are columns for filename and the starttime for each device. The issue I am running into is I only care about the latest start time for each device but the table will have multiple rows due to multiple recorded start times. Is there a way I can only get distinct results based on the device name? I can sort them by the start time, then device name to sort it with the latest but I want to remove the duplicate rows for each machine. I have looked at similar posts that use a subselect with row_number() (order by sud.StartTimeLocal) as rn
but when I tried this it just returned one row. I am creating this for a report and would prefer to find a way to only return the row with latest time for each machine instead of trying group them in the report.
Here is the query I have:
declare @ProductName as nvarchar(250) = 'Visio'
select
sy.resourceid,
sy.name0 as 'DeviceName',
sud.FileName,
sud.FileDescription,
case
substring(sud.FileVersion,1,2)
when '16' then (Sud.FileDescription + ' 2016')
when '15' then (Sud.FileDescription + ' 2013')
when '14' then (Sud.FileDescription + ' 2010')
end as 'Product',
sud.FileVersion,
sud.StartTimeLocal as 'LastExecuted'
from v_R_system sy
left join v_GS_SoftwareUsageData sud on sy.ResourceID = sud.ResourceID
where sud.FileName in (
(@ProductName + '.EXE')
)
and sud.StartNotCaptured != 1
order by sud.StartTimeLocal desc,sud.FileName,sy.Name0
Here is an example of the results i get
DeviceName FileName FileDescription Product FileVersion LastExecuted
Machine Name1 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.4993.1001 4/29/2020 10:07
Machine Name1 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.4993.1001 4/29/2020 10:07
Machine Name2 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.4954.1000 4/28/2020 11:01
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/28/2020 8:46
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/27/2020 16:20
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/27/2020 16:15
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/27/2020 15:25
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/27/2020 12:30
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/27/2020 11:51
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/27/2020 11:34
Machine Name3 VISIO.EXE Microsoft Visio Microsoft Visio 2016 16.0.11929.20648 4/27/2020 8:04
Upvotes: 0
Views: 65
Reputation: 1269793
If I had to speculate, you want one matching row from v_GS_SoftwareUsageData
for each row in the first table. Probably the best method is cross apply
:
select sy.resourceid, sy.name0 as DeviceName, sud.FileName, sud.FileDescription,
(case left(sud.FileVersion,2)
when '16' then (Sud.FileDescription + ' 2016')
when '15' then (Sud.FileDescription + ' 2013')
when '14' then (Sud.FileDescription + ' 2010')
end) as Product,
sud.FileVersion,
sud.StartTimeLocal as LastExecuted
from v_R_system sy cross apply
(select top (1) sud.*
from v_GS_SoftwareUsageData sud
where sy.ResourceID = sud.ResourceID and
sud.FileName = @ProductName + '.EXE' and
sud.StartNotCaptured <> 1
order by sud.StartTimeLocal desc, sud.FileName
) sud;
Upvotes: 1