1
Every Friday I need to generate a report that brings me transactions made in the last three months. However, I have to pick up from the 1st of each month, for example, we are in the 9th month, so I have to pick up all transactions since the 01/06 day. I always make the following consultation:
SELECT
Z.ID_CONTA
,W.ID_TRANSACAO
,Y.DS_TIPO_TRANSACAO
,W.DT_ORIGEM
,W.VL_TRANSACAO
FROM T_CONTA Z
LEFT JOIN W_TRANSACAO W ON Z.ID_CONTA = W.ID_CONTA
LEFT JOIN T_TIPOTRANSACAO Y ON W.CD_TIPO_TRANSACAO = Y.CD_TIPO_TRANSACAO
WHERE MONTH(DT_ORIGEM) >= MONTH(GETDATE()) - 3 AND YEAR(DT_ORIGEM) = 2018
It always worked, but now I’m thinking when to turn the year, so I don’t have to keep changing forever.
Your data set is
MySQL
?– Roberto de Campos
Good afternoon, it’s SQL Server.
– fercs89
Change
2018
forYEAR(GETDATE())
– Roberto de Campos
GETDATE()
returns the current date– Roberto de Campos
I already thought about it, but when it is Jan/19, I will have to bring the data of Oct/18, Nov/18, Dec/18 and Jan/19
– fercs89
@fercs89 (1) Since the column DT_ORIGEM is in table joined by LEFT JOIN, there will not always be value to compare; what to do in such cases? (2) How the column DT_ORIGEM is declared?
– José Diz
@Josédiz did not understand the question
– fercs89