-2

How can the below SQL query written using JOIN? I'm trying to convert SQL script to Trino/Presto and this does not support Cross Apply().

 select i.*,k.*

  FROM dbo.[LN] i
  CROSS APPLY(select top 1 * from
  dbo.[LN]  j
  where i.vnd_nbr = j.vnd_nbr and i.cltstyle = j.cltstyle and j.compdate > i.compdate order by compdate
  ) k order by i.VND_NBR
Martin Smith
  • 438,706
  • 87
  • 741
  • 845

1 Answers1

2

As you have a system that does not suport CROSS APPLY

xou can use a CTE for that purpüose

WITH CTE AS(
 select i.*,k.*,
 ROW_NUMBBER() OVER (PARTITION BY k.vnd_nbr,k.cltstyle, k.compdate ORDER BY k.compdate) en
  FROM dbo.[LN] i
  JOIN 
  dbo.[LN]  k
  ON i.vnd_nbr = k.vnd_nbr and i.cltstyle = k.cltstyle and k.compdate > i.compdate
  )  
  SELECT * FROM 
  CTE
  WHERE rn = 1
  order by VND_NBR
nbk
  • 45,398
  • 8
  • 30
  • 47