简体   繁体   中英

How many records were counted in specific day

Is it possible to check in DB2 how many records were counted in specific table in specific day in past

I have a table with name 'XYZ' and I would like to check row count for specific day eg for 10.09.2020, for 05.09.2020 and for 01.09.2020

In ordinary SQL, without special provisions, no, you can´t!

Depending on your usage scenario, there are several ways to achieve this function. Here are three that I can think of:

  1. If you table has a timestamp field or you can add one and you can guarantee there will be no rows deleted : You can just count the rows where the timestamp is smaller then your desired date. Cheap, performance wise, but deletes may make this impossible.

  2. You could set up a procedure that runs daily and counts your rows to write them in a different table. This van also be rather cheap from a performance point of view, but you will be limited to the specific "snapshot" times you configured beforehand and you may have conditions where the count procedure did not run an therefore data is missing.

  3. You could create an audit-table and a trigger on the table you are interested in to log every insert and delete operation on the table with a timestamp. This is the m ost performance heavy solution, but the only one that will give you always a full picture of the row count at any given time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM