How do you SUM a column without having its name?
Question
I’m stuck in a way that how to do sum on a column let’s say first column of a table (result from another query) without knowing the column name with something like column position id.
it’s something like this
select sum(what?), employID from ( select count(*), employID from table1... union all select count(*), employID from table2... union all select count(*), employID from table3... )
Or if it’s in single query (single simple select query with using sum() ) like:
select employName, sum(what?), employID from tableX
How do I tell SUM()
function to sum based on column position index in table like SUM(2)
?
Note: I don’t want to use column alias, any possibility of doing SUM not based on column name?
I know I can use column-name or aliases, but I really want to know the possibility of not using only these which is why I’m asking this question, if no possible ways, I will accept "No" as correct answer then.
asked 2018-09-22 by user108219
Answer
No. Use column names.
As best practice, always use column names. Referencing columns by index position is both brittle, and impossible to determine intent when the column index position changes.
answered 2018-09-22 by Andy Mallon