0

We have a faults table containing a column DATE_ACTIVE with a datatype of datetime2(7). Values in the this column are as follows:

2015-08-16 05:02:46.0000000
2015-08-16 05:13:25.0000000

We need to fetch records (faults) from this table which are active for more than 10 minutes and push them into another table which drives a dashboard indicating active - closed transactions. We have another column in this table which indicates the status of the fault (ACTIVE or CLOSED).

We decided to go ahead with following to achieve this:

select * 
from table 
where 
     CONVERT( float , CONVERT( datetime , getdate() ) 
     - CONVERT( datetime , DATE_ACTIVE ) ) * 24 * 60 * 60 > 600

This table keeps the record even if a record changes its state from active to closed and back to active. There is no unique ID hence for every transition from closed - active or viceversa a new row is inserted. This table keeps all the rows, nothing is deleted from this table. So the above mentioned approach gives us redundant data.

For example: a record inserted at 10 am with status CLOSED gets pushed to next table every time after 10.10 am since the above condition is always satisified till the lifetime of that row.

Is there a way to prevent this?

5
  • "we have another field in this table", which table? The one with the original faults or the one you copy them to? Also, you should easily be able to add a WHERE clause that filters out the rows already present by their primary key value. Sep 29, 2015 at 9:21
  • In both the tables. There is no primary key.
    – Umakant
    Sep 29, 2015 at 9:39
  • @Umakant maybe it could be helpful if you include the tables and some test data along with expected output Sep 29, 2015 at 9:43
  • Then add a primary key, add it with autonumber/identity in the source table and not in the other so that you can copy over the values that were generated in the source table. Sep 29, 2015 at 10:06
  • any possible combination of column to make it Unique!!, post sample records from both the table Sep 29, 2015 at 10:14

1 Answer 1

0

Short answer, unless you do not have an indicative column(s), you cannot decisively say, that two records in the dashboard table are two separate instance or the same.

I would advise you to use an ID column from the faults table in the dashboard table.

If you do not want to use the FaultsTBL.IDField, then store the time when the fetch from the faults table is done. In the next fetch from previous time till now and so on so forth.

In Conclusion, you need an indicator! Also DateDiff() is good enough to replace your existing where condition. Lastly, Fetch only faults that are still open and/or occurred after the last fetch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.