0
I have a table with values and dates referring to the cost of a vehicle. However the table is filled in as follows:
data_saida | custo_saida
-------------------------
2019-11-17 | 25
2019-11-18 | 20
null | 10
Filling is done this way because specific days are less common, and speeds up user information input.
I need that when the date is 17/11 return the cost 25, when it is 18/11 return 20, and any other days return 10 (example: 19, 20 and 21/11).
I have tried several querys and none have succeeded:
SELECT custo_saida
FROM tabela WHERE data_saida = COALESCE('2019-11-19', null::date)
The above query returns the correct cost_output when the searched date corresponds with the dates in the table, but does not return any value when the out_date is any other date, as in the example 19/11.
SELECT custo_saida
FROM tabela WHERE (data_saida = '2019-11-19' OR data_saida is null)
Already the previous query returns correct when the search date was not set in the data_output field of the table, but when I look for a date that is set, for example 18/11, it returns me the values 20 and 10.
And I need this query to be a main subquery, so it needs to return only one value.
Is there any way to do it?
By your explanation we can conclude that there may be data_output in your table that are not associated with a cost_output?
– anonimo
@anonimo In fact it is the opposite, there is costo_saida without associated data_saida. If a date that does not contain in the worksheet is informed, this outgoing data_will receive the cost_output where the outgoing data_is null. And when a data_output that contains in the worksheet is informed, it will return the value of the corresponding cost_output.
– thiagofred