Having a following table:
CREATE TABLE `foo`(
`year` INT NOT NULL,
`month` INT NOT NULL,
`day` INT NOT NULL,
`hour` INT NOT NULL,
`minute` INT NOT NULL,
`value` INT NOT NULL,
PRIMARY KEY(`year`, `month`, `day`, `hour`, `minute`)
);
I want to write a query which will add 2 columns for every record: date for standard single-column data representation and weekday to indicate day-of-a-week number.
I have tried
SELECT
`year`, `month`, `day`, `hour`, `minute`, `value`,
substr('000' || year, -4) || '-' || substr('0' || month, -2) || '-' || substr('0' || day, -2) AS `date`,
strftime('%w', `date`) AS `weekday`
FROM
`foo`;
But this says
Error: no such column: date
This is an illustration. In real I have much more complex logic in calculating additional columns and need to reuse these calculated columns to calculate other columns and just copying whole code in every place I need its value look quite scary.
Is there a way I can address a computed column value in computing another one?
Use a subquery: