Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Subject Functions Check

select * from subjects where subjectnumber='840001-006'

select * from subjectvisits where subjectid in (select subjectid from subjects


where subjectnumber='840001-006') and visitid='day14'

select * from SubjectVisitCapturedData where SubjectVisitId in (select id from


subjectvisits where subjectid in
(select subjectid from subjects where subjectnumber='840001-006') and
visitid='day14')

select * from SubjectVisitLabeledDrugs where SubjectVisitId in (select id from


subjectvisits where subjectid in
(select subjectid from subjects where subjectnumber='840001-006') and
visitid='day14')

select * from SiteDrugs where subjectid in (select subjectid from subjects


where subjectnumber='200004') and drugunitid in
('108790', '319660')

select * from SubjectProjectedVisits where subjectid in (select subjectid from


subjects where subjectnumber='200004')

select * from SiteDrugs sd left join DrugItems di on sd.DrugUnitId=di.DrugUnitId


where subjectid in (select subjectid from subjects where
subjectnumber='000001-203') and sd.drugunitid in
('20624', '20625')

System Drug Check (Subject Functions)

select * from SubjectVisitLabeledDrugs where SubjectVisitId in (select id from


subjectvisits where subjectid in
(select subjectid from subjects where subjectnumber='200010') and
visitid='rand')

select drugunitid, drugcode, internalpicknumber, lotid from drugitems where


drugunitid in ('122420', '124880', '207160', '233550', '236860')

select top 1 * from SiteDrugs sd


left join DrugItems di on sd.DrugUnitId=di.DrugUnitId
where lotid='BB4ED611-8158-11E5-8111-12587FF7CDFD' and siteid='200'
and drugstatus='2' and IsTemporaryUnavailable='0' and subjectid is null and
drugcode='1'
order by drugcode, InternalPickNumber

select * from lots where LotId='BB4ED611-8158-11E5-8111-12587FF7CDFD'


Drug Ordering

select * from drugitems where DrugUnitId in ('911722') order by PickNumber

select top 1 * from DepotDrug dd


left join DrugItems di on dd.DrugUnitId=di.DrugUnitId
where lotid='BACC1929-4B1E-11E5-AA1E-125FF2B713C2' and DrugStatus='2'
and drugcode='ol_018424'
order by drugcode,PickNumber

select * from lots where lotid='BACC1929-4B1E-11E5-AA1E-125FF2B713C2'

** SQL query to check first drug items(of each type) from a certain lot and depot, that
should be included in the next shipment(replace < mydepot> and <mylotname> with
your depot and lot; if you need more than 10 units from each drug type update
the noofdrugs variable to a higher value)

DECLARE @depot varchar(10) = '<mydepot>';


DECLARE @lotname varchar(30) = '<mylotname>';
DECLARE @noofdrugs int=10;

WITH drugs AS (
SELECT d.*, dd.DepotId, l.LotNumber, l.ExpirationDate, row_number()
OVER(PARTITION BY drugcode ORDER BY picknumber asc) AS rn FROM drugitems d
LEFT JOIN depotdrug dd ON d.drugunitid = dd.drugunitid
LEFT JOIN Lots l ON l.LotId=d.LotId
WHERE dd.DepotId=@depot AND l.LotId in(SELECT LotId FROM Lots WHERE
l.LotNumber=@lotname) AND DrugStatus=2 AND IsTemporaryUnavailable=0)
SELECT * FROM drugs WHERE rn<=@noofdrugs ORDER BY DrugCode, rn

Self support

select RandomizedSubjectsCount from study


select RandomizedSubjectsCount from sites where SiteId='123456'

select * from Subjects where subjectnumber='123456-027'

select * from subjectvisits where subjectid in (select subjectid from subjects


where subjectnumber='123456-027') and visitid='rand'

select * from SubjectVisitCapturedData where SubjectVisitId in (select id from


subjectvisits where subjectid in
(select subjectid from subjects where subjectnumber='123456-027') and
visitid='rand')

select * from SubjectProjectedVisits where subjectid in (select subjectid from


subjects where subjectnumber='123456-027')

select * from SubjectVisitLabeledDrugs where SubjectVisitId in (select id from


subjectvisits where subjectid in
(select subjectid from subjects where subjectnumber='123456-027') and
visitid='rand')

select * from SiteDrugs where SiteId='123456' and DrugUnitId='32277'


select * from DrugItems where DrugUnitId='32277'

select * from randomizationitems where RandomizationNumber='1059'


select * from RandomizationItemStatusLookup

select top 500 * from audit_trail_suvoda order by MODIFIED_DATE_TIME desc

Reports (counts)

select count(sv.id), s.SiteId from SubjectVisits sv


left join subjects s on sv.subjectid=s.SubjectId
where sv.visitid like 'rand'and sv.SubjectStatus='32'
group by s.SiteId

EDC

DECLARE @BinVar varbinary(128)

SELECT @BinVar = CAST(CAST(Id AS nvarchar(50)) AS varbinary(128)) FROM


Users WHERE Username = 'czamfir'

SET CONTEXT_INFO @BinVar

begin tran;
update notifications set IsNotificationSent='1' where title like '%edc%' and
IsNotificationSent='0'

ROLLBACK
commit

Top 5 query (from every drug type in site inventory, with the lowest pick
number)
select * from drugitems where drugunitid in (select di.drugunitid from drugitems
di where di.drugunitid in (select top 5 di2.DrugUnitId from drugitems di2
left join SiteDrugs on di2.DrugUnitId=SiteDrugs.drugunitid
where SiteId='000' and drugcode='a'
order by PickNumber ASC)
union
select di.drugunitid from drugitems di where di.drugunitid in (select top 5
di2.DrugUnitId from drugitems di2
left join SiteDrugs on di2.DrugUnitId=SiteDrugs.drugunitid
where SiteId='000' and drugcode='b'
order by PickNumber ASC)
union
select di.drugunitid from drugitems di where di.drugunitid in (select top 5
di2.DrugUnitId from drugitems di2
left join SiteDrugs on di2.DrugUnitId=SiteDrugs.drugunitid
where SiteId='000' and drugcode='x'
order by PickNumber ASC)
union
select di.drugunitid from drugitems di where di.drugunitid in (select top 5
di2.DrugUnitId from drugitems di2
left join SiteDrugs on di2.DrugUnitId=SiteDrugs.drugunitid
where SiteId='000' and drugcode='y'
order by PickNumber ASC)
union
select di.drugunitid from drugitems di where di.drugunitid in (select top 5
di2.DrugUnitId from drugitems di2
left join SiteDrugs on di2.DrugUnitId=SiteDrugs.drugunitid
where SiteId='000' and drugcode='z'
order by PickNumber ASC))
order by drugcode

-------------
Randomization items

DECLARE @BinVar varbinary(128)

SELECT @BinVar = CAST(CAST(Id AS nvarchar(50)) AS varbinary(128)) FROM


Users WHERE Username = 'czamfir'

SET CONTEXT_INFO @BinVar

begin tran;
update randomizationitems set Status='4' where
randomizationnumber='XXXXX'

ROLLBACK
commit
-----------------------------------------------------------
RAND with Stratum

with NextRandNumberPerStratum (RowNr, RandomizationNumber, StratumId,


PatientTreatment)
as
(
select row_number() over (partition by stratumid order by
randomizationnumber) as RowNr,
RandomizationNumber,
stratumid,
PatientTreatment
from RandomizationItems
where status = 1
and subjectid is null
)
select RandomizationNumber,
StratumId,
PatientTreatment
from nextRandNumberPerStratum
where RowNr = 1

----------------------------------------

Subject transactions -- subject last performed transaction

-- replace <mysubjectnumber> with your subject number

DECLARE @subjnumber varchar(30) = '<mysubjectnumber>'

select * from Subjects where SubjectNumber=@subjnumber

select top 1 * from SubjectVisits where SubjectId in(select SubjectId from Subjects
where SubjectNumber=@subjnumber) order by VisitIndexdesc

select * from SubjectVisitCapturedData where SubjectVisitId in (select top 1 id


from SubjectVisits where SubjectId in(select SubjectId from Subjects
where SubjectNumber=@subjnumber) order by VisitIndex desc)

select * from SubjectVisitLabeledDrugs where SubjectVisitId in (select top 1 id


from SubjectVisits where SubjectId in(select SubjectId from Subjects
where SubjectNumber=@subjnumber) order by VisitIndex desc)

select * from SubjectProjectedVisits where SubjectId in(select SubjectId from Subjects


where SubjectNumber=@subjnumber)

select * from SubjectProjectedDrugs where SubjectId in(select SubjectId from Subjects


where SubjectNumber=@subjnumber)

You might also like