0% found this document useful (0 votes)
3 views

Mscc-query-5mins excution time

The document contains a SQL query that retrieves detailed information about drug orders from a database, including drug codes, names, frequencies, quantities, and order statuses. It combines data from multiple tables, applying various conditions to filter the results based on visit IDs and order statuses. The final result is ordered by the date the orders were created and the type of drug order.

Uploaded by

Rayrc Pvt Ltd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Mscc-query-5mins excution time

The document contains a SQL query that retrieves detailed information about drug orders from a database, including drug codes, names, frequencies, quantities, and order statuses. It combines data from multiple tables, applying various conditions to filter the results based on visit IDs and order statuses. The final result is ordered by the date the orders were created and the type of drug order.

Uploaded by

Rayrc Pvt Ltd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

SELECT

a.*
FROM
(
SELECT
drugs.drug_gen_code AS code,
drugs.drug_gen_name AS name,
fq.frequency_name AS frequency,
fq.frequency_value AS frequencyvalue,
fq.remarks AS frequencyremarks,
do.intakequantity AS qty,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.quantity
WHEN do.isadditive = 'Y' THEN
0
ELSE
0
END AS volume,
do.noofdays AS days,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
fluidduration.profiledesc
WHEN do.isadditive = 'Y' THEN
fluidduration.profiledesc
ELSE
'Days'
END AS duration,
do.total,
nvl(do.issuedqty, 0) AS issuedqty,
lo.lookupvalue AS orderstatus,
fq.frequency_desc AS freqdesc,
do.remarks,
(
SELECT
coalesce(
sum(coalesce(id.issued_qty, 0)),
0
)
FROM
issuedetail id
WHERE
id.drugorder_id = do.orderid
) AS dispensedqty,
CASE
WHEN do.orderstatus = 643 THEN
'Processed'
WHEN do.orderstatus = 642 THEN
'Partially Processed'
WHEN do.orderstatus = 641 THEN
'Pending'
WHEN do.orderstatus = 637 THEN
'Cancelled'
WHEN do.orderstatus = 1174 THEN
'Rejected'
WHEN do.orderstatus = 805 THEN
'Discontinued'
END AS dispensedstatus,
do.drugid AS genid,
do.store AS store,
o.site_id AS siteid,
CASE
WHEN do.orderstatus = 643 THEN
3
WHEN do.orderstatus = 642 THEN
2
WHEN do.orderstatus = 641 THEN
1
WHEN do.orderstatus = 637 THEN
5
WHEN do.orderstatus = 1174 THEN
4
WHEN do.orderstatus = 805 THEN
6
END AS orderby,
do.isgeneric AS isgeneric,
do.strength AS strength,
form.lookupvalue AS drugform,
do.drugid AS drugid,
drugs.drug_gen_name
|| '-'
|| do.strength
|| '-'
|| form.lookupvalue
|| '-'
|| unit.lookupvalue itemname,
coalesce((
SELECT
dc.hideintakeqtyinprint
FROM
drugform_orderconfiguration dc
WHERE
dc.formid = form.lookupid
), 0) AS hideintakeqtyinprint,
CASE
WHEN do.ivordertype IS NOT NULL THEN
lookup.lookupvalue
ELSE
lr.lookupvalue
END AS drugroute,
do.refill,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.unitsforiv
WHEN do.isadditive = 'Y' THEN
drugno.lookupvalue
ELSE
drugno.lookupvalue
END AS drugno,
sm.profiledesc AS instruction,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.ivinfusionratevalue
WHEN do.isadditive = 'Y' THEN
do.dose
ELSE
do.dose
END AS dose,
CASE
WHEN form.lookupvalue = 'SYRUP' THEN
1
WHEN form.lookupvalue = 'OINTMENT' THEN
1
WHEN drugformsuspension.lookupvalue = 'SUSPENSION' THEN
1
ELSE
do.total
END AS totalqty,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.startdate
WHEN do.isadditive = 'Y' THEN
drugiv.strtdate
ELSE
do.startdate
END AS startdate,
do.lastdespensedate AS lastdespensedate,
--case when DO.IVORDERTYPE is not null and NVL(DO.ISADDITIVE,'N') ='N'
then o.CREATEDDATETIME
--when DO.ISADDITIVE ='Y' then drugiv.additivedate
o.createddatetime AS ordereddate,
do.isadditive AS isadditive,
drugs.drug_gen_name AS drug_gen_name,
o.order_no AS order_no,
CASE
WHEN drugiv.drugivfluidid IS NULL THEN
1
ELSE
2
END type
FROM
orders o
INNER JOIN drugorder do ON ( do.orders_id = o.order_id )
INNER JOIN druggenerics drugs ON ( drugs.drug_gen_id =
do.drugid )
INNER JOIN lookupentity lo ON ( lo.lookupid = do.orderstatus )
LEFT JOIN lookupentity unit ON ( unit.lookupid =
do.strengthunit )
LEFT JOIN frequency fq ON ( fq.frequency_id =
do.frequency_id )
LEFT JOIN lookupentity form ON ( form.lookupid = do.drugform )
LEFT JOIN lookupentity drugno ON ( drugno.lookupid =
do.drugnumber )
LEFT OUTER JOIN drugrouteofadmin dra ON dra.drugavailablerouteid =
do.routeofadmin
LEFT JOIN simpleprofiledata sm ON sm.id = do.druginstruction_id
LEFT JOIN simpleprofiledata fluidduration ON fluidduration.id =
do.ivfluidduration
LEFT OUTER JOIN lookupentity lr ON lr.lookupid = dra.drugroute
AND lr.lookupcategory = 43
LEFT JOIN lookupentity lookup ON lookup.lookupid =
do.routeforivfluid
AND lookup.lookupcategory = 43
LEFT OUTER JOIN lookupentity drugformsuspension ON
drugformsuspension.lookupid = do.drugform
LEFT JOIN drugivfluiddetails drugiv ON ( drugiv.drugorderid =
do.orderid )
WHERE
o.visit_id = :1
AND do.isgeneric = 'Y'
AND do.orderstatus IN ( 641, 642, 643 )
UNION ALL
SELECT
item.code AS code,
dgen.drug_gen_name AS name,
fq.frequency_name AS frequency,
fq.frequency_value AS frequencyvalue,
fq.remarks AS frequencyremarks,
do.intakequantity AS qty,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.quantity
WHEN do.isadditive = 'Y' THEN
0
ELSE
0
END AS volume,
do.noofdays AS days,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
fluidduration.profiledesc
WHEN do.isadditive = 'Y' THEN
fluidduration.profiledesc
ELSE
'Days'
END AS duration,
do.total,
nvl(do.issuedqty, 0) AS issuedqty,
lo.lookupvalue AS orderstatus,
fq.frequency_desc AS freqdesc,
do.remarks,
(
SELECT
coalesce(
sum(coalesce(id.issued_qty, 0)),
0
)
FROM
issuedetail id
WHERE
id.drugorder_id = do.orderid
) AS dispensedqty,
CASE
WHEN do.orderstatus = 643 THEN
'Processed'
WHEN do.orderstatus = 642 THEN
'Partially Processed'
WHEN do.orderstatus = 641 THEN
'Pending'
WHEN do.orderstatus = 637 THEN
'Cancelled'
WHEN do.orderstatus = 1174 THEN
'Rejected'
WHEN do.orderstatus = 805 THEN
'Discontinued'
END AS dispensedstatus,
dgen.drug_gen_id AS genid,
do.store AS store,
o.site_id AS siteid,
CASE
WHEN do.orderstatus = 643 THEN
3
WHEN do.orderstatus = 642 THEN
2
WHEN do.orderstatus = 641 THEN
1
WHEN do.orderstatus = 637 THEN
5
WHEN do.orderstatus = 1174 THEN
4
WHEN do.orderstatus = 805 THEN
6
END AS orderby,
do.isgeneric AS isgeneric,
do.strength AS strength,
form.lookupvalue AS drugform,
do.drugid AS drugid,
item.name AS itemname,
coalesce((
SELECT
dc.hideintakeqtyinprint
FROM
drugform_orderconfiguration dc
WHERE
dc.formid = form.lookupid
), 0) AS hideintakeqtyinprint,
CASE
WHEN do.ivordertype IS NOT NULL THEN
lookup.lookupvalue
ELSE
lr.lookupvalue
END AS drugroute,
do.refill,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.unitsforiv
WHEN do.isadditive = 'Y' THEN
drugno.lookupvalue
ELSE
drugno.lookupvalue
END AS drugno,
sm.profiledesc AS instruction,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.ivinfusionratevalue
WHEN do.isadditive = 'Y' THEN
do.dose
ELSE
do.dose
END AS dose,
CASE
WHEN form.lookupvalue = 'SYRUP' THEN
1
WHEN form.lookupvalue = 'OINTMENT' THEN
1
WHEN drugformsuspension.lookupvalue = 'SUSPENSION' THEN
1
ELSE
do.total
END AS totalqty,
CASE
WHEN do.ivordertype IS NOT NULL
AND nvl(do.isadditive, 'N') = 'N' THEN
do.startdate
WHEN do.isadditive = 'Y' THEN
drugiv.strtdate
ELSE
do.startdate
END AS startdate,
do.lastdespensedate AS lastdespensedate,
--case when DO.IVORDERTYPE is not null and NVL(DO.ISADDITIVE,'N') ='N'
then o.CREATEDDATETIME
--when DO.ISADDITIVE ='Y' then drugiv.additivedate
o.createddatetime AS ordereddate,
do.isadditive AS isadditive,
dgen.drug_gen_name AS drug_gen_name,
o.order_no AS order_no,
CASE
WHEN drugiv.drugivfluidid IS NULL THEN
1
ELSE
2
END type
FROM
orders o
INNER JOIN drugorder do ON ( do.orders_id = o.order_id )
INNER JOIN drugdetail dd ON ( dd.itemgenericid = do.drugid )
INNER JOIN genericdrugmapping dm ON dm.itemgenericid = dd.itemgenericid
INNER JOIN druggenerics dgen ON dgen.drug_gen_id =
dm.druggenerics
INNER JOIN item item ON ( item.id = dd.item_refid )
LEFT JOIN lookupentity unit ON ( unit.lookupid =
do.strengthunit )
INNER JOIN lookupentity lo ON ( lo.lookupid = do.orderstatus )
LEFT JOIN frequency fq ON ( fq.frequency_id =
do.frequency_id )
LEFT JOIN lookupentity form ON ( form.lookupid = do.drugform )
LEFT JOIN lookupentity drugno ON ( drugno.lookupid =
do.drugnumber )
LEFT OUTER JOIN drugrouteofadmin dra ON dra.drugavailablerouteid =
do.routeofadmin
LEFT JOIN simpleprofiledata sm ON sm.id = do.druginstruction_id
LEFT JOIN simpleprofiledata fluidduration ON fluidduration.id =
do.ivfluidduration
LEFT OUTER JOIN lookupentity lr ON lr.lookupid = dra.drugroute
AND lr.lookupcategory = 43
LEFT JOIN lookupentity lookup ON lookup.lookupid =
do.routeforivfluid
AND lookup.lookupcategory = 43
LEFT OUTER JOIN lookupentity drugformsuspension ON
drugformsuspension.lookupid = do.drugform
LEFT JOIN drugivfluiddetails drugiv ON ( drugiv.additivedrugorderid =
do.orderid )
WHERE
o.visit_id = :2
AND do.isgeneric = 'N'
AND do.orderstatus IN ( 641, 642, 643 )
) a
ORDER BY
a.ordereddate DESC,
type ASC

You might also like