How do you return the max value of several columns (date and cost relationship)?
TableName [ID, Date1, Cost1, Date2, Cost2, Date3, Cost3]
I need to return something like this: [ID, Most Recent Date, Cost]
I need to return something like this:
- ID
- Most Recent Date
- the Cost related to the Date (if the most recent date is Date1 so the Cost is Cost1)
I need to use another table – BackupTableName:
- ID
- Date1
- Cost1
- Date2
- Cost2
- Date3
- Cost3
…and use its’ data in the situation when Cost1, Cost2 and Cost3 from TableName are null.
‘Date1’…‘Date2’…‘Date3’ is a schema smell. Consider normalising to a separate many-to-one table.
ETA:
You have nulls in there too? OK, this is getting really unpleasant. You’d need to protect against the nulls, like:
(in both the date-selection bit and the cost-selection bit. This assumes a Cost will always normally be higher than 0.)
Well… it’s possible but your model is now looking extremely precarious.
If it is at all within your power you need to normalise these tables, because the data model you have at the moment is essentially broken.