I am implementing audit trail in my application. I followed this and this created History table the tables that only needs to be audited.
I have a following tables to store the Patient information.
TenantId represent each tenant data, since its a multitenant application
Person
and Patient
table
Person
Id, TenantId, FirstName, LastName, DOB, Mobile, Email,AddedBy,UpdatedBy, IsDeleted
Patient
Id, PatientIDentifier, IsOP, CanSendSMS, AddedBy, UpdatedBy, IsDeleted
Also another table
Appointment
Id, PatientId, AppointmentDate, DoctorId, Price, AddedBy, UpdatedBy
AUDIT HISTORY TABLE STRUCTURE
PersonHistory
AuditId,Id, FirstName, LastName, DOB, AddedBy, UpdatedBy, AuditUserId, AuditDate, Action
Here Action
represents A(ADD)/U(update)/D(delete)
The same structure has been created for Patient, Appointment
Now I got triggers to insert into History table whenever add/delete/update happens.
Now I got the data available in the audit history table.
I have to write a query for two requirements.
Get all the records for a perticular patient. I need to pull all the records from PatientHostory, AppointmentHistory, PersonHistory by using a
PatientId
. How do we write SQL that takes more records from same table for given id?UNION or JOIN
?I need to take all the records from
all the HistoryTable for the supplied AuditUser Id
.
How can i write a query for this?