i use Order By Used with Case Statement and may be this is the reason for which i am getting error Windowed functions can only appear in the SELECT or ORDER BY clauses.
here is my complete sql where i use Row_Number function for pagination with sorting. please guide me where i made the mistake.
DECLARE #StartIndex INT
DECLARE #EndIndex INT
DECLARE #SortColumn VARCHAR(MAX)
DECLARE #SortDirection VARCHAR(MAX)
SET #StartIndex = 1
SET #EndIndex = 20
SET #SortColumn = 'OrderID'
SET #SortColumn = 'A'
SELECT * FROM Orders
WHERE
ROW_NUMBER() OVER
(
ORDER BY
CASE (#SortColumn + ':' + #SortDirection)
WHEN 'OrderID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID ASC)
WHEN 'OrderID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID DESC)
WHEN 'CustomerID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.CustomerID ASC)
WHEN 'CustomerID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.CustomerID DESC)
WHEN 'EmployeeID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.EmployeeID ASC)
WHEN 'EmployeeID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.EmployeeID DESC)
WHEN 'OrderDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderDate ASC)
WHEN 'OrderDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderDate DESC)
WHEN 'RequiredDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.RequiredDate ASC)
WHEN 'RequiredDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.RequiredDate DESC)
WHEN 'ShippedDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID ASC)
WHEN 'ShippedDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID DESC)
WHEN 'ShipVia:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipVia ASC)
WHEN 'ShipVia:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipVia DESC)
WHEN 'Freight:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.Freight ASC)
WHEN 'Freight:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.Freight DESC)
WHEN 'ShipName:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipName ASC)
WHEN 'ShipName:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipName DESC)
WHEN 'ShipAddress:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipAddress ASC)
WHEN 'ShipAddress:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipAddress DESC)
WHEN 'ShipCity:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCity ASC)
WHEN 'ShipCity:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCity DESC)
WHEN 'ShipRegion:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipRegion ASC)
WHEN 'ShipRegion:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipRegion DESC)
WHEN 'ShipPostalCode:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipPostalCode ASC)
WHEN 'ShipPostalCode:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipPostalCode DESC)
WHEN 'ShipCountry:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCountry ASC)
WHEN 'ShipCountry:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCountry DESC)
END
) BETWEEN (#StartIndex - 1) * #EndIndex + 1 AND #StartIndex * #EndIndex
/* AND more conditions ... */
ORDER BY
CASE WHEN #SortDirection = 'A' THEN
CASE #SortColumn
WHEN 'OrderID' THEN OrderID
WHEN 'CustomerID' THEN CustomerID
WHEN 'EmployeeID' THEN CustomerID
WHEN 'OrderDate' THEN CustomerID
WHEN 'RequiredDate' THEN CustomerID
WHEN 'ShippedDate' THEN CustomerID
WHEN 'ShipVia' THEN CustomerID
WHEN 'Freight' THEN CustomerID
WHEN 'ShipName' THEN CustomerID
WHEN 'ShipAddress' THEN CustomerID
WHEN 'ShipCity' THEN CustomerID
WHEN 'ShipRegion' THEN CustomerID
WHEN 'ShipPostalCode' THEN CustomerID
WHEN 'ShipCountry' THEN CustomerID
END
END,
CASE WHEN #SortDirection = 'D' THEN
CASE #SortColumn
WHEN 'OrderID' THEN OrderID
WHEN 'CustomerID' THEN CustomerID
WHEN 'EmployeeID' THEN CustomerID
WHEN 'OrderDate' THEN CustomerID
WHEN 'RequiredDate' THEN CustomerID
WHEN 'ShippedDate' THEN CustomerID
WHEN 'ShipVia' THEN CustomerID
WHEN 'Freight' THEN CustomerID
WHEN 'ShipName' THEN CustomerID
WHEN 'ShipAddress' THEN CustomerID
WHEN 'ShipCity' THEN CustomerID
WHEN 'ShipRegion' THEN CustomerID
WHEN 'ShipPostalCode' THEN CustomerID
WHEN 'ShipCountry' THEN CustomerID
END
END DESC
EDIT 1
i change my code as per your one but still getting same error. here is new code
DECLARE #StartIndex INT
DECLARE #EndIndex INT
DECLARE #SortColumn VARCHAR(MAX)
DECLARE #SortDirection VARCHAR(MAX)
SET #StartIndex = 1
SET #EndIndex = 20
SET #SortColumn = 'CustomerID'
SET #SortDirection = 'D'
;WITH cte as(SELECT *,
ROW_NUMBER() OVER
(ORDER BY
CASE (#SortColumn + ':' + #SortDirection)
WHEN 'OrderID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID ASC)
WHEN 'OrderID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID DESC)
WHEN 'CustomerID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.CustomerID ASC)
WHEN 'CustomerID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.CustomerID DESC)
WHEN 'EmployeeID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.EmployeeID ASC)
WHEN 'EmployeeID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.EmployeeID DESC)
WHEN 'OrderDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderDate ASC)
WHEN 'OrderDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderDate DESC)
WHEN 'RequiredDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.RequiredDate ASC)
WHEN 'RequiredDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.RequiredDate DESC)
WHEN 'ShippedDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID ASC)
WHEN 'ShippedDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID DESC)
WHEN 'ShipVia:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipVia ASC)
WHEN 'ShipVia:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipVia DESC)
WHEN 'Freight:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.Freight ASC)
WHEN 'Freight:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.Freight DESC)
WHEN 'ShipName:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipName ASC)
WHEN 'ShipName:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipName DESC)
WHEN 'ShipAddress:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipAddress ASC)
WHEN 'ShipAddress:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipAddress DESC)
WHEN 'ShipCity:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCity ASC)
WHEN 'ShipCity:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCity DESC)
WHEN 'ShipRegion:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipRegion ASC)
WHEN 'ShipRegion:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipRegion DESC)
WHEN 'ShipPostalCode:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipPostalCode ASC)
WHEN 'ShipPostalCode:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipPostalCode DESC)
WHEN 'ShipCountry:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCountry ASC)
WHEN 'ShipCountry:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.ShipCountry DESC)
END
) rn
FROM Orders)
SELECT * FROM cte
WHERE rn BETWEEN (#StartIndex - 1) * #EndIndex + 1 AND #StartIndex * #EndIndex
As you have already figured it out you can not use window function in where clause. Just do it in cte or subquery and then filter it outside:
;WITH cte as(SELECT *,
ROW_NUMBER() OVER
(ORDER BY CASE WHEN (#SortColumn + ':' + #SortDirection) = 'OrderID:A'
THEN Orders.OrderID ASC END,
CASE WHEN (#SortColumn + ':' + #SortDirection) = 'OrderID:D'
THEN Orders.OrderID DESC END,
...) rn
FROM Orders)
SELECT * FROM cte
WHERE rn BETWEEN (#StartIndex - 1) * #EndIndex + 1 AND #StartIndex * #EndIndex
/* AND more conditions ... */
ORDER BY
...
--same case expressions here
As you have noticed instead of one big case expression you will have to have many because you can not use different types in one case expressions as you will get conversion errors.
But from my experience it is better to have dynamic sql with parametrised parameters because sql engine will save the execution plans for each case and will be much more quick then having such a big complicated query.
Dynamic version:
DECLARE #sql NVARCHAR(MAX)
SET #sql = ';WITH cte as(SELECT *,
ROW_NUMBER() OVER
(ORDER BY ' + CASE (#SortColumn + ':' + #SortDirection)
WHEN 'OrderID:A' THEN 'Orders.OrderID ASC'
WHEN 'OrderID:D' THEN 'Orders.OrderID DESC'
...
WHEN 'ShipCountry:D' THEN 'Orders.ShipCountry DESC' END +
') rn
FROM Orders)
SELECT * FROM cte
WHERE rn BETWEEN ' + CAST((#StartIndex - 1) * #EndIndex + 1 AS NVARCHAR(MAX)) + ' AND ' +
CAST(#StartIndex * #EndIndex AS NVARCHAR(MAX))
EXEC(#sql)
Related
select * from
(select * except(rn) from (select sistema_productivo, cod_mpio, periodo,
has_sembrada,has_sembrada_und, id_cultivo,anio, row_number() over(partition by cod_mpio order by
anio desc) rn from TABLENAME where SUBSTR(cod_mpio,1,2) in("23") and sistema_productivo in("ARROZ
SECANO MANUAL", "MAIZ TECNIFICADO") and periodo in("B")) t where rn = 1 and anio =(select max(anio)
as date_max, from (select * except(rn) from (select sistema_productivo, cod_mpio, periodo,
has_sembrada,has_sembrada_und, id_cultivo,anio, row_number() over(partition by cod_mpio order by
anio desc) rn from TABLENAME where SUBSTR(cod_mpio,1,2) in("23") and sistema_productivo in("ARROZ
SECANO MANUAL") and periodo in("B")) t where rn = 1))
)
This query returned
How Can I to get the total sum of has_sembrada from "MAIZ TECNIFICADO" and "ARROZ SECANO MANUAL"
select sistema_productivo, sum(has_sembrada) as total from
(....)
group by sistema_productivo
I have this sql query :
update CCUSTOMERINFO set VALIDTO=sysdate where (
select * from (
select row_number() over (order by created desc) rn, customer_id, CCUSTOMERINFO.VALIDTO
from CCUSTOMERINFO
where customer_id=100309772 order by created DESC) where rn > 1);
But it say it have some mistake.
This query returns all i want to update :
select * from (
select row_number() over (order by created desc) rn, customer_id, CCUSTOMERINFO.VALIDTO
from CCUSTOMERINFO
where customer_id=100309772 order by created DESC) where rn > 1)
Any suggestion how can i do that?
Use it like the below
update CCUSTOMERINFO set VALIDTO=sysdate where rowid in (
select row_id from (
select row_number() over (order by created desc) rn, customer_id, rowid row_id,
CCUSTOMERINFO.VALIDTO
from CCUSTOMERINFO
where customer_id=100309772 order by created DESC) where rn > 1);
I have a SQL table with the columns: ID, ADate, XValue and Type. For each "Type" value, I'd like to retrieve the most recent row (per ADate), the least recent row (per ADate) and also the row containing the highest "XValue" value.
Is it possible to be performed through a single SQL query? I've tried a few times with no success.
Using row_number() you could do something like this:
select ID, ADate, XValue and Type
from (
select *
, min_adate = row_number() over (partition by type order by adate asc)
, max_adate = row_number() over (partition by type order by adate desc)
, max_xvalue = row_number() over (partition by type order by xvalue desc)
from t
) as sub
where 1 in (min_adate,max_adate,max_xvalue)
/* --also could be written as
where min_adate = 1
or max_adate = 1
or max_xvalue = 1
*/
Using a common table expression may make it more readable.
;with cte as (
select *
, min_adate = row_number() over (partition by type order by adate asc)
, max_adate = row_number() over (partition by type order by adate desc)
, max_xvalue = row_number() over (partition by type order by xvalue desc)
from t
)
select ID, ADate, XValue and Type
from cte
where 1 in (min_adate,max_adate,max_xvalue)
select ID, ADate, XValue,[Type]
from (
select *
,row_number() over (partition by type order by adate asc) AS DateSeq
,row_number() over (partition by type) AS TypeCount
,row_number() over (partition by type order by xvalue desc) AS ValueSeq
from t
) as tt
where tt.DateSeq=1 OR tt.DateSeq=tt.TypeCount OR tt.ValueSeq=1
One method uses row_number():
select t.*
from (select t.*,
row_number() over (partition by type order by aDate desc) as seqnum_adate_desc,
row_number() over (partition by type order by aDate asc) as seqnum_adate_asc,
row_number() over (partition by type order by XValue desc) as seqnum_xvalue_desc
from t
) t
where 1 in (seqnum_adate_desc, seqnum_adate_asc, seqnum_xvalue_desc);
I have this working query
SELECT MIN([DealerCode])
,[201309]
,RowNum = Row_Number() OVER(Order By [201309])
,Rnk = RANK() OVER(ORDER BY [201309])
,DenseRnk = DENSE_RANK() OVER(ORDER BY [201309])
,NTile4 = NTILE(100) OVER(ORDER BY [201309])
,BM = RANK() OVER(ORDER BY [201309])*0.7
FROM [SA_Sew].[dbo].[Sew_YTD_Composite$]
where Ratio_ID = 'fi02u' AND DealerCode like '%VW%'
GROUP BY [201309];
How would I be able to get the NTile4 calculation as a field to be able to use it in the Where Field? NTile4 = 70 is benchmark and I need to only see the benchmark.
Thanks!
Write as:
select * from
(
SELECT MIN([DealerCode]) as MinDealerCode
,[201309]
,RowNum = Row_Number() OVER(Order By [201309])
,Rnk = RANK() OVER(ORDER BY [201309])
,DenseRnk = DENSE_RANK() OVER(ORDER BY [201309])
,NTile4 = NTILE(100) OVER(ORDER BY [201309])
,BM = RANK() OVER(ORDER BY [201309])*0.7
FROM [SA_Sew].[dbo].[Sew_YTD_Composite$]
where Ratio_ID = 'fi02u' AND DealerCode like '%VW%'
GROUP BY [201309]
) as T
where NTile4 = 70;
I have a table with 200,000 rows. I have created a View where I am removing slices of data from this table based on different criteria which fit my definition of what constitutes a duplicate record. I have the code for doing so below and I was wondering if anyone could suggest a faster/more efficient method of writing this query. It currently takes about 20 seconds to execute but I was hoping for a couple of seconds at most to execute this query if not less. I am using SQL Server 2005. My knowledge of SQL is very beginner and I appreciate any help.
WITH dsm_hardware_basic_cte AS
(
SELECT TOP 100 PERCENT
dbo.dsm_hardware_basic.[UUID]
,dbo.dsm_hardware_basic.[Name]
,dbo.dsm_hardware_basic.[LastAgentExecution]
,dbo.dsm_hardware_basic.[MaxUserRegistration]
,REPLACE(RIGHT([MaxUserRegistration], CHARINDEX('/', REVERSE([MaxUserRegistration])) - 1),'_ADMIN','') AS [MaxUserUsername]
,dbo.dsm_hardware_basic.[LastUserRegistration]
,REPLACE(RIGHT([LastUserRegistration], CHARINDEX('/', REVERSE([LastUserRegistration])) - 1),'_ADMIN','') AS [LastUserUsername]
,dbo.dsm_hardware_basic.[IPAddress]
,dbo.dsm_hardware_basic.[HostName]
,dbo.dsm_hardware_basic.[MACAddress]
FROM dbo.dsm_hardware_basic
)
SELECT TOP 100 PERCENT
dsm_hardware_basic_cte.[UUID]
,dsm_hardware_basic_cte.[Name]
,dsm_hardware_basic_cte.[LastAgentExecution]
,dsm_hardware_basic_cte.[MaxUserRegistration]
,dsm_hardware_basic_cte.[LastUserRegistration]
,dsm_hardware_basic_cte.[IPAddress]
,dsm_hardware_basic_cte.[HostName]
,dsm_hardware_basic_cte.[MACAddress]
FROM dsm_hardware_basic_cte
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [MACAddress] ORDER BY [LastAgentExecution] DESC) AS [NameMACRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameMAC_filtered
ON duplicate_NameMAC_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameMAC_filtered.[NameMACRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [HostName] ORDER BY [LastAgentExecution] DESC) AS [NameHostNameRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameHostName_filtered
ON duplicate_NameHostName_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameHostName_filtered.[NameHostNameRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [HostName], [MACAddress] ORDER BY [LastAgentExecution] DESC) AS [HostNameMACRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_HostNameMAC_filtered
ON duplicate_HostNameMAC_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_HostNameMAC_filtered.[HostNameMACRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [HostName], [IPAddress] ORDER BY [LastAgentExecution] DESC) AS [HostNameIPAddressRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_HostNameIPAddress_filtered
ON duplicate_HostNameIPAddress_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_HostNameIPAddress_filtered.[HostNameIPAddressRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [MaxUserUsername] ORDER BY [LastAgentExecution] DESC) AS [NameMaxUserRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameMaxUser_filtered
ON duplicate_NameMaxUser_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameMaxUser_filtered.[NameMaxUserRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [LastUserUsername] ORDER BY [LastAgentExecution] DESC) AS [NameLastUserRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameLastUser_filtered
ON duplicate_NameLastUser_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameLastUser_filtered.[NameLastUserRowNum] = 1
I don't know what your needs are, but I'd try re-writing the query as such:
WITH dsm_hardware_basic_cte AS (
SELECT
d.[UUID]
,d.[Name]
,d.[LastAgentExecution]
,d.[MaxUserRegistration]
,REPLACE(RIGHT([MaxUserRegistration], CHARINDEX('/', REVERSE([MaxUserRegistration])) - 1),'_ADMIN','') AS [MaxUserUsername]
,d.[LastUserRegistration]
,REPLACE(RIGHT([LastUserRegistration], CHARINDEX('/', REVERSE([LastUserRegistration])) - 1),'_ADMIN','') AS [LastUserUsername]
,d.[IPAddress]
,d.[HostName]
,d.[MACAddress]
,ROW_NUMBER() OVER (PARTITION BY [Name], [MACAddress] ORDER BY [LastAgentExecution] DESC) AS [NameMACRowNum]
,ROW_NUMBER() OVER (PARTITION BY [Name], [HostName] ORDER BY [LastAgentExecution] DESC) AS [NameHostNameRowNum]
,ROW_NUMBER() OVER (PARTITION BY [HostName], [MACAddress] ORDER BY [LastAgentExecution] DESC) AS [HostNameMACRowNum]
,ROW_NUMBER() OVER (PARTITION BY [HostName], [IPAddress] ORDER BY [LastAgentExecution] DESC) AS [HostNameIPAddressRowNum]
,ROW_NUMBER() OVER (PARTITION BY [Name], [MaxUserUsername] ORDER BY [LastAgentExecution] DESC) AS [NameMaxUserRowNum]
,ROW_NUMBER() OVER (PARTITION BY [Name], [LastUserUsername] ORDER BY [LastAgentExecution] DESC) AS [NameLastUserRowNum]
FROM dbo.dsm_hardware_basic as d
)
SELECT
c.[UUID]
,c.[Name]
,c.[LastAgentExecution]
,c.[MaxUserRegistration]
,c.[LastUserRegistration]
,c.[IPAddress]
,c.[HostName]
,c.[MACAddress]
FROM dsm_hardware_basic_cte as c
WHERE
c.[NameMACRowNum] = 1
or c.[NameHostNameRowNum] = 1
or c.[HostNameMACRowNum] = 1
or [HostNameIPAddressRowNum] = 1
or [NameMaxUserRowNum] = 1
or [NameLastUserRowNum] = 1
I think that your query and mine are logically equivalent. The optimizer might be smart enough to have reduced your query to mine, but give it a spin and see! A couple of notes:
I used table aliases in order to make it a bit more readable (in my opinion)
I removed the "top 100 percent" clause from your select. It's not needed; that's typically a hack that people put in place so they can do an order by in the view to get, well, an "ordered view". Don't do that. :)
according to your Query Plan the sorting by LastAgentExecution takes 19% of time. Start by creating an index on this column.
However, if I were you I would changed the habit of using "ROW_NUMBER() OVER (PARTITION BY [Name], [MACAddress] ORDER BY [LastAgentExecution] DESC)" type of syntax, because it does not seem to be very effective
Instead of inner joins, try replacing them with the "Exists" clause like this
WHERE EXISTS
((SELECT [UUID],[NameMACRowNum]
FROM
(SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [MACAddress] ORDER BY [LastAgentExecution] DESC) AS [NameMACRowNum]
FROM dsm_hardware_basic_cte) AS duplicate_NameMAC_filtered
WHERE duplicate_NameMAC_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameMAC_filtered.[NameMACRowNum] = 1)
Not sure it should be Exists or Not Exists, but it will be simple to change once the rest is working.