I have an in-memory table defined as follows in DolphinDB:
t = table(2025.01.01 as date, 1 as M01, 2 as M02, 3 as M03, 4 as M04, 5 as M05, 6 as M06, 1 as M07, 2 as M08, 3 as M09, 4 as M10, 5 as M11, 6 as M12)
/*
date M01 M02 M03 M04 M05 M06 M07 M08 M09 M10 M11 M12
2025.01.01 1 2 3 4 5 6 1 2 3 4 5 6
*/
Here, date is a date field, and M01 to M12 represent monthly counts for each month of the year.
I am trying to use meta-programming to calculate the differences between consecutive months for each row. My code is as follows:
leg1Month = t.columnNames()[1: (11 + 1)] // Returns vector [M01, M02, ..., M11]
leg2Month = t.columnNames()[2: (11 + 2)] // Returns vector [M02, M03, ..., M12]
columns = leg1Month + " - " + leg2Month // Returns vector [M01 - M02, M02 - M03, ..., M11 - M12]
columnAlias = "\"" + leg1Month + "-" + leg2Month + "\""
sqlCode = <select date, _$$columns as _$$columnAlias from t>
sqlCode.eval()
However, when executing the last line, I get the following error:
eval(sqlCode) => Unrecognized column name [M01 - M02].
I checked the value of sqlCode:
sqlCode
// Output:
code('< select date, M01 - M02 as "M01-M02", M02 - M03 as "M02-M03", M03 - M04 as "M03-M04", M04 - M05 as "M04-M05", M05 - M06 as "M05-M06", M06 - M07 as "M06-M07", M07 - M08 as "M07-M08", M08 - M09 as "M08-M09", M09 - M10 as "M09-M10", M10 - M11 as "M10-M11", M11 - M12 as "M11-M12" from t >')
The generated meta-code appears correct. If I directly execute the following SQL:
select date, M01 - M02 as "M01-M02", M02 - M03 as "M02-M03", M03 - M04 as "M03-M04", M04 - M05 as "M04-M05", M05 - M06 as "M05-M06", M06 - M07 as "M06-M07", M07 - M08 as "M07-M08", M08 - M09 as "M08-M09", M09 - M10 as "M09-M10", M10 - M11 as "M10-M11", M11 - M12 as "M11-M12" from t
I get the desired result. Why does sqlCode.eval() throw an error?