Using SQL variables in sum () rows causes unpredictable results

Source: Internet
Author: User

Query 1)

Select * From test;
No1 NO2
1 1
2 2
3 3
4 4
5 5

Query 2)

select @wokao:= (no1 + no2), @wokao from test group by no1;
No1 NO2
2 2
4 4
6 6
8 8
10 10

Query 3)

select @wokao:= (no1 + no2), sum(@wokao) from test group by no1;
No1 NO2
2 Null
4 2
6 4
8 6
10 8

The result of the third SQL query is strange, in theory it should get the same results as Query2, and why the results are different.

I asked this question in StackOverflow.

The answer is big. In the third SQL query, the sum (@wokao) is executed first, followed by @wokao:= (No1 + NO2), which results in the above result.

Written with Stackedit.

Using SQL variables in sum () rows causes unpredictable results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.