On 2018/01/07 10:11 PM, Bart Smissaert wrote:
Sure, in that case there can be no sensible column name.
In the great majority of cases though the select field will be of a single
table column, with or without
an expression. In those cases it will be helpful to get the non alias
To repeat an answer by Richard given for a recent other question (if a
How much extra code, resources and memory are you willing to spend for
ALL future queries so that you can know the sometimes available original
column used in an alias?
I would prefer that answer to be "Zero".
"One would think that if sqlite3_column_name can get the alias name then
somehow it must
be possible to get the real column name."
One would be wrong if one would think that. Inside the code/engine there
are no column names, only pointers, (in fact I think the engine has to
make special memory space for storing the output names - it is this
memory that gets interrogated when you call sqlite3_column_name()), the
names only exist as a method for humans to state the intent of the
question - it is forgotten at earliest convenience if not directly
intended for output, and those that do get added to output has to be
collected/deduced at a non-zero resource cost already. (At least this is
my current understanding, I might have it wrong).
Btw. - I am quite sure this holds for all DB engines, but if there is an
engine out there that do let you trace back, I'd be interested to know.
Also, the assumption about the "vast majority of cases" is simply wrong.
The vast majority of returned column names in queries are not aliases,
indeed aliases are mostly used when the column value is a result of a
function or combination of columns (such as Simon's reply suggested)
where you need a good resulting reference name in the output. Are you
using aliases for columns where they are not needed?
Further to this, there is nothing wrong with copying the name in the
alias, such as: SELECT MAX(ColA) AS ColA FROM ....
Lastly, even if we involve zero column joining or functions, plain
aliasing is a problem by itself. Consider this next query, what do you
feel is the true original name of the output column sq1?
CREATE VIEW v1 AS SELECT c1 AS vc1 FROM (SELECT 'X' AS c1);
SELECT sq1 FROM (SELECT vc1 AS sq1 FROM v1);
HTH, and I hope this adequately illustrates why your request is not
possible currently, not trivial to implement and also not widely requested.
sqlite-users mailing list