UPDATE pv_user
SET locked = True, 1deleted FROM= pv_userTrue
WHERE
pv_user.id IN (
-- Patient
SELECT A.id
FROM
pv_user A
INNER JOIN pv_user_group_role B ON a.id = B.user_id
WHERE
B.role_id = 1
)
AND
pv_user.id NOT IN (
SELECT X.id
FROM
pv_user X
INNER JOIN pv_user_group_role Y ON X.id = Y.user_id
INNER JOIN pv_group Z ON Y.group_id = Z.id
WHERE
-- Patient
Y.role_id = 1 AND
-- Unit
Z.type_id = 1 AND
(
-- Scotland
Z.code LIKE 'S%'
OR
-- Edinburgh
Z.code IN ('EDINHF1', 'RSC02')
OR
-- Northern Ireland
Birmingham Paeds Z.code = 'RQ3' IN ('24027', '2020', '11023', '33020', '7021', '45021', '48021', '45020')
OR
-- BristolBirmingham Paeds
Z.code = 'RA723RQ3'
OR
-- LeedsBristol Paeds
Z.code = '99RQR13RA723'
OR
-- LiverpoolLeeds Paeds
Z.code = 'RBS2599RQR13'
-- OR
-- EvelinaLiverpool (RJ122)Paeds
Not on PV Z.code = 'RBS25'
OR -- GOSHOR
Z.code = 'RP4'-- Evelina (RJ122) Not on PV
OR
-- Manchester Paeds
Z.code = 'RW3RM'
-- OR
-- Newcastle Paeds (RTDO2) Not on PV
OR
-- Nottingham Paeds
Z.code = '99RCSLB'
OR
-- Southampton Paeds
Z.code = '99RHM01'
OR
-- Ipswich
Z.code = 'RGQ02'
OR
-- MSENottingham
Z.code IN ('RAJRCSLB', 'RAJ01', 'RQ8L0', 'RDDH0'RX1CC')
OR
-- NottinghamIBD
Z.code IN= ('RCSLB', 'RX1CC')'SALIBD'
OR
-- BristolDiabetes
Z.code = 'REE01DSF01'
)
)
AND
-- Exclude OR
-- Gloucester
z.code = 'RH641'
OR
-- Preston
Z.code = 'RMF01'
OR
-- IBD
Z.code = 'SALIBD'
OR
-- Diabetes
Z.code = 'DSF01'
)
AND
-- Exclude Paeds
age(now(), date_of_birth) >= '18 years'Paeds
age(now(), date_of_birth) >= '18 years'
AND
-- Get a Count of how many locked
locked = False; |