I have a working query in T-SQL and I was attempting to use it on a programme to upload data from my SQL Server to the programme for reporting purposes however I found out that the programme does not support T-SQL and it mainly uses ANSI. Would anyone be able to help me find an alternative to the 'TOP 1 WITH TIES' part of the query for ANSI?
I am trying to get the newest cost prices for all products from different tables and I need the WITH TIES so as to not have the query exclude basically everything as a most of the products have the same price as others.
Here is the current query that works perfectly (To my knowledge) in T-SQL:
SELECT TOP 1 with ties
LastCostDate as LastDate,
SiteID,
[d1].UnitsInOuter,
[PLUGroup2].Description as GroupDescription,
[d1].PLUID,
[d1].Description,
Cost,
Null as Price,
Null as Units,
Null as RetailValue
FROM
(select LastCostDate,
[LocStock].SiteID,
[LastDate].UnitsInOuter,
[LastDate].PLUID,
[LastDate].Description,
[LastCost].Cost
From (select max([PLUCostHistory].ChangeDate) as LastCostDate,max(PLUOuterSize.UnitsInOuter) as UnitsInOuter, [PLUCostHistory].PLUID, [PLU].Description From [PLUCostHistory] Inner Join [PLU]
on [PLU].PLUID=[PLUCostHistory].PLUID Inner Join PLUOuterSize on PLUOuterSize.PLUOuterSizeID=PLUCostHistory.PLUOuterSizeID group by [PLUCostHistory].PLUID, [PLU].Description ) as LastDate
Inner Join (select [PLUCostHistory].ChangeDate, [PLUCostHistory].PLUID, [PLU].Description, sum([PLUCostHistory].Cost) as Cost From [PLUCostHistory] Inner Join [PLU]
on [PLU].PLUID=[PLUCostHistory].PLUID group by [PLUCostHistory].ChangeDate, [PLUCostHistory].PLUID, [PLU].Description ) as LastCost
On LastDate.Description=LastCost.Description
And LastDate.LastCostDate=LastCost.ChangeDate
And LastDate.PLUID=LastCost.PLUID
Inner Join [LocStock]
on [LocStock].PLUID=[LastDate].PLUID
UNION
select LastCostDate,
[DelMast].SiteID,
[LastDate].UnitsInOuter,
[LastDate].PLUID,
[LastDate].Description,
[LastCost].Cost
From (select max([DelMast].DeliveryDate) as LastCostDate,max(PLUOuterSize.UnitsInOuter) as UnitsInOuter, [DelDets].PLUID, [PLU].Description From [DelMast] Inner Join [DelDets]
on [DelDets].DeliveryID=[DelMast].DeliveryID Inner Join [PLU] on [PLU].PLUID=[DelDets].PLUID Inner Join [PLUGroupRef]
on [DelDets].PLUID=[PLUGroupRef].PLUID Inner Join [PLUGroup2]
on [PLUGroup2].PLUGroup2ID=[PLUGroupRef].PLUGroup2ID Inner Join PLUOuterSize on PLUOuterSize.PLUOuterSizeID=DelDets.PLUOuterSizeID group by [DelDets].PLUID, [PLU].Description) as LastDate
INNER Join (select [DelMast].DeliveryDate, [DelDets].PLUID, [PLU].Description, sum([DelDets].Cost) as Cost From [DelMast] Inner Join [DelDets] on [DelDets].DeliveryID=[DelMast].DeliveryID Inner Join [PLU]
on [PLU].PLUID=[DelDets].PLUID group by [DelMast].DeliveryDate, [DelDets].PLUID, [PLU].Description) as LastCost
On LastDate.Description=LastCost.Description
And LastDate.LastCostDate=LastCost.DeliveryDate
And LastDate.PLUID=LastCost.PLUID
Inner Join [LocStock]
on [LocStock].PLUID=[LastDate].PLUID
Inner Join [DelMast]
on [LocStock].SiteID=[DelMast].SiteID ) as d1
Inner Join [PLUGroupRef]
on [PLUGroupRef].PLUID=[d1].PLUID
Inner Join [PLUGroup2]
on [PLUGroup2].PLUGroup2ID=[PLUGroupRef].PLUGroup2ID
/*where SiteID like '%BALYS%' and [PLUGroup2].Description like '%TOBACCO%' */
ORDER BY ROW_NUMBER() OVER (PARTITION BY [d1].Description,SiteID ORDER BY LastCostDate DESC)