February 12, 2025 at 5:50 pm
How to rewrite a query in MS SQL, please?
WHERE a.ROWID IN (SELECT rid
FROM ( SELECT ROWID rid,
row_number() OVER (PARTITION BY c.id ORDER BY c.id) rn
FROM details c
WHERE a.id = c.id
AND c.id > ( SELECT max(id)
FROM details d
WHERE d.id = c.id
AND d.cd = '2030')
AND c.cd IN ( '2017','2012','2021'))
WHERE rn = 1 )
February 13, 2025 at 6:10 pm
Thanks for posting your issue and hopefully someone will answer soon.
This is an automated bump to increase visibility of your question.
February 14, 2025 at 11:15 am
This was removed by the editor as SPAM
February 14, 2025 at 12:26 pm
This was removed by the editor as SPAM
February 16, 2025 at 4:50 am
In MS SQL Server, there is no direct equivalent to Oracle’s ROWID.
However, there are several alternatives depending on what you need to achieve:
WHERE a.id IN ( SELECT id FROM ( SELECT c.id, ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY c.id) AS rn FROM details c WHERE a.id = c.id AND c.id > ( SELECT MAX(id) FROM details d WHERE d.id = c.id AND d.cd = '2030' ) AND c.cd IN ('2017', '2012', '2021') ) AS subquery WHERE rn = 1 )
February 24, 2025 at 6:44 am
Here's how to rewrite the query in a concise and easy-to-understand way in MS SQL:
WITH CTE AS (
SELECT
c.ROWID AS rid,
ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY c.id) AS rn
FROM details c
WHERE c.id > (
SELECT MAX(d.id)
FROM details d
WHERE d.id = c.id AND d.cd = '2030'
)
AND c.cd IN ('2017', '2012', '2021')
)
SELECT a.*
FROM your_table a
JOIN CTE ON a.ROWID = CTE.rid
WHERE CTE.rn = 1 AND a.id = CTE.id;
February 26, 2025 at 8:52 am
This was removed by the editor as SPAM
Viewing 0 posts
You must be logged in to reply to this topic. Login to reply