17

What is the correct syntax to get the over clause to work in mysql?

I would like to see the total sms's sent by each user without grouping it with the group by clause.

SELECT 
    username, 
    count(sentSmsId) OVER (userId) 
FROM 
    sentSmsTable,
    userTable
WHERE
    userId = sentUserId;
2
  • what kind of result do you want to have through this query? Commented Jun 9, 2011 at 12:23
  • I would like to see the total sms's sent by each user without grouping it with the group by clause. Commented Jun 9, 2011 at 12:25

4 Answers 4

21

MySQL 8 has got the window functions! Therefore, you can write your query in it like this:

SELECT username, 
       count(sentSmsId) OVER (partition by userId) 
FROM sentSmsTable
JOIN userTable ON userId = sentUserId;     
Sign up to request clarification or add additional context in comments.

Comments

13

There is no OVER clause in MySQL that I know of, but here is a link that might assist you to accomplish the same results:

http://explainextended.com/2009/03/10/analytic-functions-first_value-last_value-lead-lag/

Hope this helps.

1 Comment

This is no longer true for MySQL 8: dev.mysql.com/doc/refman/8.0/en/window-functions.html
5

MySQL does not currently support window functions, so over() will only yield syntax errors (or garbage, if it's accepted regardless).

Comments

3

MySQL Doesn't have window functions until the most recent release: MySQL 8 (release in April, 2018). MS SQL Server also accepts OVER clause.

The syntax is:

function(col1) OVER (PARTITION BY col2 ORDER BY col3)

Check out https://mysqlserverteam.com/mysql-8-0-2-introducing-window-functions/ for more examples.

Comments

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.