C# - Check if dates are missing in the selected period

Asked

Viewed 81 times

1

I have the following SQL

SELECT 
    dt_finished::date AS "DataFinalizacao",
    count(DISTINCT tba.id_batches) AS "Quantidade Total"
FROM tb_batches AS tba
INNER JOIN tb_routes tro ON (tro.fk_id_products = tba.fk_id_products 
    AND tro.fk_id_sessions = 70 
    AND tba.finished = TRUE)
WHERE tba.dt_finished::date <= now()::date 
    AND tba.dt_finished::date >= (now() - INTERVAL '15 day')::date
GROUP BY tba.dt_finished::date
ORDER BY tba.dt_finished::date DESC

I need to return every day they’ve had batch completed, if there is no batch finished that day should bring the day only with the total amount zeroed. The problem is he’s not bringing the date.

How to treat this in C# or in SQL?

printscreen com a resultset de uma consulta

My class

public class TaxaProducaoBLL : ITaxaProducaoBLL
    {
        public IEnumerable<TaxaProducaoOV> CalcularTaxaProducao(IEnumerable<TaxaProducaoOV> taxaProducao)
        {
            foreach (var taxa in taxaProducao)
            {

            }
            return new List<TaxaProducaoOV>();
        }
    }

1 answer

2

In reality you have to use LEFT JOIN, that there you bring the results independent if it has relationship with the tb_routes and the COUNT() will return 0.

The INNER JOIN you say you want to take everything from tb_batches who is in the tb_routes

  • The scheme is that the following, there is nothing save this date missing in the bank, I have to check in the period of the select dates if there are missing dates , and put these dates ... I do not know if I understand well...

  • ah understood, this is more complex you would have to have an auxiliary table of dates, for the first query to be in it then. this way it only takes the records that exist in the table

  • Got it, I think q using an auxiliary table would be better msm :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.