Select from previous months

Asked

Viewed 4,389 times

2

I need to do select data from previous months, month by month.

My query is like this:

SELECT *
  FROM VIEW_INCD
 WHERE MONTH(DH_CRIA_INCD) = DATEPART(mm,GETDATE()) - 1
   AND YEAR(DH_CRIA_INCD) = YEAR(GETDATE());

It works, I select only from the previous month, and if I need to see the previous one, I put "- 2", "- 3", etc....

The problem is when I need to see December, because the year changes and the query does not return anything. How do I make the year track this subtraction of months?

  • You need to see all dates below the current date starting from the previous month?

  • all previous months of the current date, one query per month

  • If you need to inform the subtraction amount every time, isn’t it easier to inform the date you want to see in the case of the month/year? I could not understand very well why it is this way, this is in a Procedure? The query is always done directly in the database ?

  • I’m assembling a Dashboard in PHP, which shows in a table the amount of problems of the current month (I already did), and up to 4 previous months. Yes, straight from the bank.

  • The problem is that the query I did, it only works if it is the same year, if a person is going to use Dashboard in January next year for example, will only be able to see the current month, and the previous 4 months will be reset because the year is different

  • Note: it is month by month, quantity of current month: x, quantity of previous month: x, etc..

  • So is there a limitation of at most 4 months ago? (I ask this, because this information is relevant to form a solution).

  • For this Dashboard yes, will always show the amount of problems of the current month, until 4 previous months

Show 3 more comments

4 answers

2

To get the data for just one month, try

-- código #1
-- informe número de meses a retroagir
declare @RetroMes tinyint;
set @RetroMes= 1;

-- calcula período mensal de consulta
declare @DataInicial date, @DataFinal date;
set @DataInicial= dateadd(month, datediff(month, 0, current_timestamp) - @RetroMes, 0);
set @DataFinal= dateadd(month, +1, @DataInicial); 

--
SELECT colunas
  from VIEW_INCD
  where DH_CRIA_INCD >= @DataInicial 
        and DH_CRIA_INCD < @DataFinal;

I haven’t tried it; I hope there are no mistakes.


If you want to return the entire period in a single query, try the following code:

-- código #2
-- informe número de meses do período
declare @RetroMes tinyint;
set @RetroMes= 4;

-- calcula período mensal de consulta
declare @DataInicial date, @DataFinal date;
set @DataFinal= dateadd(month, datediff(month, 0, current_timestamp), 0);
set @DataInicial= dateadd(month, -@RetroMes, @DataFinal);

--
SELECT year(DH_CRIA_INCD) as Ano, 
       month(DH_CRIA_INCD) as Mês,
       colunas
  from VIEW_INCD
  where DH_CRIA_INCD >= @DataInicial 
        and DH_CRIA_INCD < @DataFinal;
  • Then in PHP I would put all this in the $query variable?

  • Like searching for data with PHP, you can use declare and/or set?

2

We need to filter the last 4 months (as per your comments) from the current month of the system.

We use the function DATEADD(MONTH, -4, CONVERT(date, GETDATE())) to subtract 4 months from our current date and use the CONVERT(date, GETDATE()) to ignore the schedule. Observing: If you want to consider the schedule, change the CONVERT(date, GETDATE()) for GETDATE(). If you need to change the amount of months for the last 5, 6, 7... months change the parameter -4 for the amount of days (negatively, as we are subtracting the months) desired -5, -6, -7.

We use the function DATEADD(MONTH, -1, CONVERT(DATE, GETDATE())) to ignore the current month.

SELECT *
  FROM VIEW_INCD
 WHERE DH_CRIA_INCD BETWEEN DATEADD(MONTH, -4, CONVERT(date, GETDATE())) AND DATEADD(MONTH, -1, CONVERT(DATE, GETDATE()))
 ORDER BY DH_CRIA_INCD DESC

To run a query just for 1 month as you said in the comments, try running the command below:

SELECT *
  FROM VIEW_INCD
 WHERE DH_CRIA_INCD BETWEEN DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0)
   AND DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))
 ORDER BY DH_CRIA_INCD DESC

The first part of BETWEEN we are spending the first day of the previous month (because of the -1).

Observing: If you want to change the add-1, -2, -3 month after the DATEADD function, but the -1 refers to the previous 1 month, the -2 refers to previous 2 months, and so on.

Example: Whereas GETDATE() will return the day 08/08/2018, the function below will return 01/07/2018.

 DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0)

The second part of BETWEEN we are spending the last day of the previous month.

Observing: If you want to change the add month -1, -2, -3 after the DATEADD function, but the -1 refers to 2 previous months, the -2 refers to 3 previous months, and so on. Example: Whereas GETDATE() will return the day 08/08/2018, the function below will return 30/06/2018.

DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0))
  • Do you follow the year? If I run this Query in January, does it search for the previous months of the previous year? And tbm can not do a query for every month, said it is month by month

  • Accompanies the year yes, if you inform for example 01/01/2018 and ask -4 months, will return you until the month 09 of 2017. What do you mean, a month-by-month query? I don’t understand.

  • A query that returns only the data of a month, regardless of whether it is the previous month, or 2 months ago. The month that should show, will be defined in the query

  • I changed the answer including also the "query month by month", I will here also a link to another question where I explain how to search the last day of the month. Link: https://answall.com/questions/319818/sql-updated-value/319829#319829

1

I would subtract the date in php and pass to the database the desired month and year of query, preferably in a Procedure.

As an alternative I made a query based on yours that will allow you to obtain the desired result, it is worth mentioning that this solution is limited to four months that is your need.

Sqlfiddle - Example:

SELECT * 
FROM VIEW_INCD
WHERE 
  MONTH(DH_CRIA_INCD) = 
  CASE DATEPART(mm,GETDATE()) -8
    WHEN 0  THEN 12
    WHEN -1 THEN 11
    WHEN -2 THEN 10
    WHEN -3 THEN 9
  ELSE
    DATEPART(mm,GETDATE()) - 8
  END
AND 
  YEAR(DH_CRIA_INCD) =
  CASE WHEN DATEPART(mm,GETDATE()) - 8 < 1 THEN
    YEAR(GETDATE()) - 1
  ELSE
    YEAR(GETDATE())
  END

1


Very simple using DATEFROMPARTS:

In the WHERE:

Parte esquerda: we are transforming the current date on the first day of the month and unpacking it the "@par" number of times.

Parte direita: turning the column date into the first day of the month.

And compare the two :)

Just replace @par with the number of months: -1, -2...

SELECT * FROM VIEW_INCD WHERE dateadd(month, @par, DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1)) = DATEFROMPARTS(YEAR(DH_CRIA_INCD), MONTH(DH_CRIA_INCD), 1)

  • 1

    I have 13 problems in the previous month, this query returned 6874 records when I put -1 rsrsrsr

  • Strange, what is the format of your table? Here I use it quietly. Let me see if I did some typo.

  • Run like this, see if the comparisons are making sense (those of the month you want have to have the two columns equal): SELECT top(100) dateadd(month, @par, DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1)) as left,&#xA; DATEFROMPARTS(YEAR(DH_CRIA_INCD), MONTH(DH_CRIA_INCD), 1) as right&#xA; FROM VIEW_INCD&#xA; order by DH_CRIA_INCD desc

Browser other questions tagged

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