Check items in history N:N

Asked

Viewed 38 times

0

Currently I have a basic asset control system with the following structure:

inserir a descrição da imagem aqui

As you can see, I have a Many-To-Many table, which is called TB_HISTORICO_ATIVO. It has all the history the asset has spent, so I have the following status:

  • Stockpile
  • Client
  • Maintenance
  • Burned

The problem is occurring at the time I pull the report by client.

Example:

An asset number X was on the customer on 01/01/2010, but was for maintenance on 01/01/2012, this implies that this asset, is no longer on the customer but, when I do my query, it always takes the date 01/01/2010, because it appears in the table and has the client code.

My select:

SELECT
a.CODIGO as 'codigo_ativo',
a.NUMERO_ATIVO,
a.valor_compra,
a.obs as 'obs_ativo',

p.CODIGO as 'codigo_produto',
p.descricao as 'produto',

h.data_evento,
h.obs as 'obs_historico',

c.codigo as 'codigo_cliente',
c.descricao as 'cliente' 

from TB_HISTORICO_ATIVO H

left outer join TB_ATIVO a on H.ATIVO = a.CODIGO

left outer join TB_PRODUTO p on a.PRODUTO = p.CODIGO

left outer join TB_CLIENTE c on H.CLIENTE = c.CODIGO

where H.CLIENTE = 2
  • probably your query should be returning the two records, and you always want the last status following the chronological order. Right?

  • No, he’s just returning the record that’s tied up with the client, but in reality, he’s returning the past status, in this case, it wasn’t to return anything.

  • I’m lost in this issue, rs, I’m not able to solve. I can even solve inside the application using LINQ and everything, but I want to leave this task of filtering the data within the query itself.

  • A solution to simplify sqls can be to create a column in TB_ATIVO date of the last history (DT_ULTIMO_HISTORICO) and by Trigger feed this column, otherwise make sqls with subselect and max to obey the latest history

  • Interesting!... This solution to create the field and work with a Rigger. But, what would select look like with MAX()? I tried N ways and none worked.

  • Ball show @Motta, I created this field, migrated the information, and in this select I passed above, I simply implemented in the filter a comparison by update date! (y) Thanks.

  • with 'max" would be something like .... select * from historico active Where = 123 data = (select max(data) from historico active Where = 123)

  • In this case, I wouldn’t have the asset parameter, just the client.

Show 3 more comments
No answers

Browser other questions tagged

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