1
One more challenge stopped in my hands. I need help in my query.
What I need:
I want to know who are the people who have had account releases on a given day, where I need to show the value and situation of that day.
Being so, I have two tables, one that has the lasso of the person and the other that I have the values and the situation.
Table and data
This table is a history that stores information according to modifications, not necessarily occur every day.
Histpessoa Table
PessoaId | Situacao | DataInsert | Valor
999 | Ativo | 2018-01-10 | 1111
999 | Ativo | 2018-01-12 | 2222
999 | Ativo | 2018-01-13 | 3333
999 | Inativo| 2018-01-18 | 4444
999 | Ativo | 2018-01-20 | 5555
999 | Inativo| 2018-01-22 | 5555
999 | Ativo | 2018-01-25 | 6666
Table Lancamento
PessoaId | LacamentoId | DataLancamento | Descrocao
999 | 0001 | 2018-01-17 | Lacamento 1
999 | 0002 | 2018-01-25 | Lacamento 2
999 | 0003 | 2018-01-24 | Em divida com o sistema, liberação negada
999 | 0002 | 2018-01-25 | Lacamento 2 estornado
General remarks:
The Histpessoa table keeps a hostile all the time valor
or situação
is changed. Through the release date I need to inform the situation and the person’s value on the day of launch.
Example: On January 17, 2018 the person 999 was in situation Active with value of 3333. And on January 25, 2018 person 999 was in a situation Active with value of 6666
At launch 0001 a DataLancamento (2018-01-17)
does not exist in the table HistPessoa
, in that case I should take the DataInsert
previous to DataLacamento
closer than the date 2018-01-13
HistPessoa
has stored long-standing history, it is a very large table. It would be interesting to have an optimization.
Expected result
PessoaId | LacamentoId | DataLacamento | Situacao | Valor
999 | 0001 | 2018-01-17 | Ativo | 3333
999 | 0002 | 2018-01-25 | Ativo | 6666
What I’ve done so far
SELECT TOP 100 PERCENT
ROW_NUMBER() OVER (PARTITION BY PessoaId ORDER BY DataInsert DESC) AS NUM,
PessoaId,
valor,
Situacao
FROM HistPessoa
ORDER BY DataInsert DESC
This query sorts in descending order, but does not filter by date so it picks the last date inserted in the table HistPessoa
.
Extra
I’ve assembled something more complete based on my real solution. Where I leave by Lancamento
in search of the other data.
Follow the link in the most complete SQL Fiddle. HERE
Any tips? Or possible solution?
Maybe with CTE or Gaps And Island?
Puts an example of the expected result
– Tiedt Tech
SELECT * FROM HistPessoa WHERE PessoaId = @Lancamento_PessoaId AND DataInsert <= @Lancamento_DataLancamento ORDER BY DataInsert DESC
? (you would enter the desired values where you are '@Lancamento_') or you want the result of the two tables together?– Pedro Gaspar
@Marlontiedt added. For both types of Releases
– EmanuelF
@Pedrogaspar-Lobofx I edited the question and put the expected results. I need to make a Join between the tables
– EmanuelF
I understood very well your doubt no, but if you reformulate I can help you. I think remarks is a little confused to understand.
– Marconi
@Marconi was really bad. You can understand now?
– EmanuelF
@Emanuelf looks at my answer!
– Marconi
@Marconi I edited again, added the link to a new SQL Fiddle based on what you’ve assembled.
– EmanuelF
@Emanuelf already researched on and CTE will have the same processing of a sub-colony.
– Marconi
Let’s go continue this discussion in chat.
– Marconi