简体   繁体   中英

SQL Query : Sum(@variable)

Could someone tell me please why there is a difference between the two last column i get when i run this simple query?

In this example, the last column is the same as the third shifted back one position with a first row which contains NULL .

SELECT 
  SUM(PRICE), 
  @sum := SUM(PRICE) , 
  ln(SUM(PRICE)), 
  ln(@sum) 
FROM 
  TABLE 
group by ISIN

That's because the @sum variable is assigned the value after the values from each group are created. In the last column the current value of @sum is used to create the value, so that will be what was assigned from the previous group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM