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?
You would UNION
the log data, also consider adding a HistoryType
column to indicate which table it refers too
SELECT AuditId, Id, FirstName, LastName, DOB, AddedBy, UpdatedBy,
AuditUserId, AuditDate, Action, 'Person' HistoryType
FROM PersonHistory ph
JOIN Person p ON ph.id = p.id
UNION ALL
SELECT AuditId,Id, FirstName, LastName, DOB, AddedBy, UpdatedBy,
AuditUserId, AuditDate, Action, 'Patient' HistoryType
FROM PatientHistory
UNION ALL
SELECT AuditId,Id, FirstName, LastName, DOB, AddedBy, UpdatedBy,
AuditUserId, AuditDate, Action, 'Appointment' HistoryType
FROM AppointmentHistory
Turning the above into a VIEW
would allow you to further query the data more easily
SELECT * FROM vAuditHistory
WHERE AuditUserId = 1234
If you need to get information from the 'originating' data ie PatientId
s then again using the view you could do something along the lines of
DECLARE @PatientId VARCHAR(10) = '12345ABCDE'
SELECT h.*, p.PatientId
FROM vAuditHistory h
JOIN Patient pt ON pt.id = h.id
WHERE h.HistoryType = 'Patient'
AND pt.PatientId = @PatientId
UNION ALL
SELECT h.*, a.PatientId
FROM vAuditHistory h
JOIN Appointment a t ON a.id = h.id
WHERE h.HistoryType = 'Appointment'
AND pt.PatientId = @PatientId
or without the view
SELECT h.*, p.PatientId
FROM PatientHistory h
JOIN Patient pt ON pt.id = h.id
WHERE pt.PatientId = @PatientId
UNION ALL
SELECT h.*, a.PatientId
FROM AppointmentHistory h
JOIN Appointment a t ON a.id = h.id
WHERE pt.PatientId = @PatientId
Creating the above as an inline function would probably be useful
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.