-3

I have this MySQL table:

id Name
1 Spanish
2 French
3 German

I am using this statement:

SELECT id, Name, Lead(Name) OVER (ORDER BY id) AS 'next' from MyTable WHERE Name='French'

The expected result is this:

id Name next
2 French German

The result is this:

id Name next
2 French (NULL)
8
  • It is because there is no "next" record for id=2. There's only one. Perhaps you didn't really want the OVER clause? What were you expecting here? Commented Jul 7, 2024 at 18:35
  • Tim, from what I have read, 'Over' is a mandatory part of the 'Lead' function. Ordered by 'id', the next row should be 'German'. Commented Jul 7, 2024 at 18:50
  • 1
    @Cymro LEAD is calculated after WHERE. Commented Jul 7, 2024 at 18:56
  • Use HAVING name = 'French' to perform the filter after getting the result set. Commented Jul 7, 2024 at 19:07
  • @Barmar Won't help. The resultset is formed after HAVING is applied (and its creation takes into account this HAVING clause effect). Commented Jul 7, 2024 at 19:22

1 Answer 1

3

I'm not an SQL expert, but this works:

With e AS (
    SELECT id, Name, Lead(Name) OVER (ORDER BY id) AS 'next' from MyTable 
) SELECT Id, Name, next FROM e WHERE Name='French'

There may be more efficient solutions, but since the table is about languages I don't expect it to contain millions of records. Therefore I think performance will hardly be an issue.

/Edit: This code does the same and I suppose that it will be indeed more efficient for large tables since it will not first calculate all 'next' fields for the entire table:

SELECT d.*, e.Next
    FROM MyTable d OUTER APPLY
    (SELECT TOP(1) Name AS Next
    FROM Mytable e 
    WHERE e.Id > d.Id
    ORDER BY Id
    ) AS e 
    WHERE d.Name = 'French'
Sign up to request clarification or add additional context in comments.

8 Comments

That worked. Would have been cool to have a solution in a single select in line with the official syntax, but thanks nontheless.
@Cymro "The official 'Lead'" works exactly as it should. The function would be utterly useless if it was calculated before WHERE. Same for all other window functions. It is your expectations that are wrong, not the result of the function or its documentation.
@Cymro, I think you don't get GSerg's point: your code first selects a data set where Language = 'French' (containing only 1 record), and then calculates 'next' (which does not exist for a set of 1 record).
@Lex. you didn't even use 'Lead' in your solution, which supports my statement that the official SQL syntax for the Lead function is crap. I got -3 points for this? I should be a hero.
The official syntax for LEAD is pathetic. It should be just like in my question, just like it is for other SQL functions. Example here: w3schools.com/sql/sql_sum.asp
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.