0
I am working on a table that keeps the history of some people, and I need to identify and display the lines that have been modified in a certain column.
See example:
--Tabela HistSituacaoPessoa
id | nome | situacao | data
999| Fulano | Ativo | 2017-12-08 23:59:06.947
999| Fulano | Ativo | 2017-12-09 23:59:06.947
999| Fulano | Inativo | 2017-12-10 23:59:06.947
999| Fulano | Inativo | 2017-12-11 23:59:06.947
999| Fulano | Inativo | 2017-12-12 23:59:06.947
999| Fulano | Cobranca | 2017-12-13 23:59:06.947
999| Fulano | Ativo | 2017-12-14 23:59:06.947
999| Fulano | Ativo | 2017-12-14 23:59:06.947
999| Fulano | Inativo | 2017-12-15 23:59:06.947
I need to know the line that hears the modification, in the first case shows the line that has Active, then you must show me the line that has Inactive, the next one I want to know is the line that appears the charge. With the above table example the data to be displayed should be the following:
--Valores que devem ser exibidos baseado na tabela acima
999| Fulano | Ativo | 2017-12-08 23:59:06.947
999| Fulano | Inativo | 2017-12-10 23:59:06.947
999| Fulano | Cobranca | 2017-12-13 23:59:06.947
999| Fulano | Ativo | 2017-12-14 23:59:06.947
999| Fulano | Inativo | 2017-12-15 23:59:06.947
I want to know the dates that changed a person’s situation. What way can I make the query?
I could not understand what you want! What has changed in the lines that the
SELECT
should return? The situation? What if you store the date of the last query somewhere and do thatSELECT
based on the stored date?– Roberto de Campos
I edited it. What I want back is to know the dates a person had changed in the situation. Knowing that if Fuluna was active on the 10th and 12th he was inactive. Of course returning all the columns of the query.
– EmanuelF
From what I understand he wants to know what has changed in a table and who has changed and when. Essentially it would be a log? Well, in this case I imagine creating a history table, and filling it with triguers or procs as the main table changes
– Joe Torres
@Joetorres I consider as historical, because the table is being updated every day during the year.
– EmanuelF
I get it. Well you can do something like that https://docs.microsoft.com/pt-br/sql/relational-databases/track-changes/track-data-changes-sql-server where there is a replication of data. He’ll take any and all changes you make. Or if you just want to cosisa specifics, put ma Trigger in the table that saves in another table what changes have been made. or if access to this table is done by procedures only, save in these history tables the relevant changes. Anyway, it’s a reasonable but not impossible job.
– Joe Torres
Dude I’m not gonna put as an answer because I’m not sure it’s gonna work in your case, but try it with this
SELECT
:– Roberto de Campos
SELECT id, nome, situacao, DATA FROM HistSituacaoPessoa
GROUP BY nome
ORDER BY DATA DESC
– Roberto de Campos
@Robertodecampos Thank you very much! But I found another solution.
– EmanuelF
@Thank you! I found the solution.
– EmanuelF
Why give me negative?
– EmanuelF