I have a Leave table as per below:
MONTH | NAME | DAY1 | DAY2 | DAY3 | ... | DAY31
------------------------------------------------
| | | | | ... |
Columns DAY1 to DAY31 keep the number of hours the named person has not been in the office.
I need a query to generate this output:
MONTH | NAME | TOTAL_MONTH_HOURS
---------------------------------
| |
where TOTAL_MONTH_HOURS is sum of all hours in the month; i.e. DAY1 + DAY2 + ... + DAY30+DAY31
How should I write this query? Is there any way to escape having to sum all the columns as I did above? I encounter similar problems from time to time where tables get even bigger!
Note: some columns may be NULL due to variant month lengths.
The simplest, given the current table structure, is simply to do exactly that –
Now, if it is possible to have NULLs and that being the reason for the question, wrap them with COALESCE. COALESCE is part of the ANSI SQL99 standard and has support on all major RDBMS (MySQL Oracle SQLite PostgreSQL SQL Server).