Include extremes in BETWEEN and List missing values in SQL query

Asked

Viewed 475 times

4

Next. Imagine I have in the bank in the paid months field the following releases: 2015-03, 2015-04, 2015-7, 2015-9. If we set a period for example: from 2015-02 to today (2015-9). Which months are missing, that is, which months have not yet been paid.

The query I present here brings paid months WITHIN period. But what I need ARE NOT the months paid, but YES the UNPAID MONTHS and WITHIN period. Ie 2015-2, 2015-5, 2015-6, 2015-8.

 SELECT pagamentos.idPagamentos, pagamentos.mesReferencia FROM pagamentos, planosclientes
 WHERE
        pagamentos.idPlanoClientes = planosclientes.idPlanosClientes AND
        pagamentos.idPlanoClientes = 8 AND
        pagamentos.mesReferencia BETWEEN DATE_FORMAT(planosclientes.dataInstalacao, '%Y%-%m') AND DATE_FORMAT(CURRENT_DATE(), '%Y%-%m')
 ORDER BY pagamentos.mesReferencia

How to do it in this case?

  • Carlos I don’t think I understand what you need, you want the extremes, wouldn’t it just be using the < and > signals instead of between? because I believe that there is no extreme after the Kurdish() on that date, or has ?

  • I changed the question to make it easier to interpret!

2 answers

1

A possible solution is to use DATEADD, except a month of dataInstalacao and adding a month on CURRENT_DATE().

SELECT pagamentos.idPagamentos, pagamentos.mesReferencia 
FROM pagamentos, planosclientes
WHERE pagamentos.idPlanoClientes = planosclientes.idPlanosClientes 
   AND pagamentos.idPlanoClientes = 8 
   AND pagamentos.mesReferencia 
      BETWEEN FORMAT(DATEADD(mm,-1,planosclientes.dataInstalacao), 'yyyy-MM') 
      AND FORMAT(DATEADD(mm,1,CURRENT_DATE()), 'yyyy-MM') ;

UPDATE 2

Function DATE_FORMAT and DATE_ADD (for MYSQL):

BETWEEN DATE_FORMAT(DATE_ADD(planosclientes.dataInstalacao, -1 INTEVAL MONTH), '%Y-%M') 
    AND DATE_FORMAT(DATE_ADD(CURRENT_DATE, INTERVAL 1 MONTH), '%Y-%M')

However, BETWEEN includes the limits, that is, it would not be necessary to make any date adjustments.

But I realized you’re doing between varcharwhat will not work.

So you must do the CAST for values to be compared and for that the code would look like this:

AND CAST(CONCAT(pagamentos.mesReferencia, '01') as DATE)
   BETWEEN CAST(CONCAT(FORMAT(planosclientes.dataInstalacao), '%Y-%m'), '01') as DATE) 
   AND CURRENT_DATE()
  • That mm, -1 this error damage: ...#1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '-1,planosclientes. dataInstallation), '%Y%-%m') AND DATE_FORMAT(DATE_ADD(mm,1,CURRE' at line 5

  • In your comment the code is wrong... I also adjusted the answer. pagamentos.mesReferencia BETWEEN format(dateadd(mm,-1,dataInstalacao), 'yyyy-MM')&#xA; AND format(dateadd(mm,1,GETDATE()), 'yyyy-MM')

  • Mysql Message: Documentation #1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '-1,planosclientes. dataInstallacal), 'y-m') AND FORMAT(DATEA_DD(mm,1,CURRENT_DATE(' at line 5

  • updated the question showing the error that gives!

  • Carlos, I made a mistake... Now that I see that you are using Mysql. The code I put in the answer is for SQL. In Mysql BETWEEN includes the limits. Optionally you can use the operators >= and <=. The function DATE_ADD mysql uses the following syntax (example using your code): DATE_ADD(planosclientes.dataInstalacao, INTERVAL 1 MONTH)

0

As far as I understand it, you want to < those > <-----------> right? Come on:

     SELECT pagamentos.idPagamentos, pagamentos.mesReferencia FROM pagamentos, planosclientes
 WHERE
        pagamentos.idPlanoClientes = planosclientes.idPlanosClientes AND
        pagamentos.idPlanoClientes = 8 AND
        pagamentos.mesReferencia < DATE_FORMAT(planosclientes.dataInstalacao, '%Y%-%m') AND 
        pagamentos.mesReferencia > DATE_FORMAT(CURRENT_DATE(), '%Y%-%m')
 ORDER BY pagamentos.mesReferencia;

That is to say the smallest datastalacao and the largest > than the kurdish() (present day) so we will have the so-called "extremes"

  • No! Next.Imagine I have on the bench in the field of months paid the following releases: 2015-03, 2015-04, 2015-7, 2015-9. If we stipulate a period for example: from 2015-02 to today (2015-9). Which months are missing, that is, which months have not yet been paid. To query that I presented at the end of the question brings paid months WITHIN period. What I need are the months NAY paid and WITHIN period. Ie 2015-2, 2015-5, 2015-6, 2015-8. Improved?

  • I get it, Monday I do some SQL tests so

Browser other questions tagged

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