Professional Documents
Culture Documents
Queries
Queries
** 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)
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
Reports (counts)
EDC
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
begin tran;
update randomizationitems set Status='4' where
randomizationnumber='XXXXX'
ROLLBACK
commit
-----------------------------------------------------------
RAND with Stratum
----------------------------------------
select top 1 * from SubjectVisits where SubjectId in(select SubjectId from Subjects
where SubjectNumber=@subjnumber) order by VisitIndexdesc