List only contracts due by ID and do not consider if ID has contract in force

Asked

Viewed 34 times

0

Fala galera...

I need to list only the EXPIRED contracts, because when the company renews the contract, the data of the expired (old) contract remain in the BD with status "0". So we have: 1 contract expired (old) and 1 in force (new, which was renewed).

And this query is showing me the company that has expired contract registration, but that already has contract in force (renewed) and this is not right, because if she has already renewed the contract, then there is no expired contract on the current date and then it should not appear in the list.

What I need is a list of companies that do not have contract current and loser.

I tried that query, but it didn’t work:

SELECT *

FROM contrato_empresa ce, empresas e

WHERE 

e.emp_id = ce.cte_id_emp AND
ce.cte_status = 0 AND
ce.cte_dt_ini < CURDATE() AND
ce.cte_dt_fim < CURDATE()

ORDER BY e.emp_cidade ASC, e.emp_fantasia ASC

Someone can help me?

Thank you!

  • If you want to filter out companies that don’t have existing and expired contracts, you want companies that don’t have contracts?

  • @Diego_f, I need the list only with EXPIRED contract, because when the company renews the contract, the data of the expired (old) contract remain in the BD with status "0". So we have: 1 expired contract (old) and 1 in force (new, which has been renewed). And this query is showing me the company that has expired contract registration, but that already has contract in force (renewed) and this is not right, because if she has already renewed the contract, then there is no expired contract on the current date and then it should not appear on the list, He drew?! :)

1 answer

0

First problem in your query, within the where there is that ce.cte_id = ce.cte_id , What’s the point of comparing something to itself? There is no logic, maybe the problem is a c the more, getting ce.cte_id = e.cte_id, but I still believe that there is no such verification because it does not meet the second normal form.

Second problem,

You do a validation that the contract has a shorter start date than the current date, OK ce.cte_dt_ini < CURDATE() But this contract also needs to have a shorter contract end date than the current date, that is to say it needs to be ce.cte_dt_fim > CURDATE() and not ce.cte_dt_fim < CURDATE() as in your query. Your query should then look like this

SELECT *
FROM contrato_empresa ce, empresas e
WHERE 
e.emp_id = ce.cte_id_emp AND
ce.cte_status = 0 AND
ce.cte_dt_ini < CURDATE() AND
ce.cte_dt_fim > CURDATE() AND
ORDER BY e.emp_cidade ASC, e.emp_fantasia ASC
  • Alexandre, your suggestion displays the contracts in force with status "0" and that’s not what I want. I want the EXPIRED contract list, because when the company renews the contract, the data of the expired contract is in the BD with status "0". So we have: 1 expired contract and 1 in effect (new, renewed). And this query is showing me the company that has expired contract registration, but that already has contract in force (renewed) and this is not right, because if she has already renewed the contract, then has no contract expired on the current date, Drew?! :)

  • This difficult to understand the explanation of what you want. I understand that a contract is renewed at the expiration, or after the expiration, of the previous contract. This way all contracts already expired will obviously be expired at the current date.

  • So if you want an expired query, you don’t need a start date, because the important thing is to be bigger than the end date &#xA;SELECT *&#xA;FROM contrato_empresa ce, empresas e&#xA;WHERE &#xA;e.emp_id = ce.cte_id_emp AND&#xA;ce.cte_status = 0 AND&#xA;ce.cte_dt_fim < CURDATE() AND&#xA;ORDER BY e.emp_cidade ASC, e.emp_fantasia ASC&#xA;

Browser other questions tagged

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